1.增删
public class ff {
public static void main(String[] args) {
String str = "leet**cod*e";
StringBuilder sb = new StringBuilder();
// char[]chs = str.toCharArray();
for (int i = 0; i < str.length(); i++) {
char ch = str.charAt(i);
if (ch != '*') {
sb.append(ch);
} else {
sb.deleteCharAt(sb.length() - 1);
}
}
System.out.println(sb.toString());
}
}
2.线程与进程
2.1 多线程
public class ThreadDemo01 extends Thread{
@Override
public void run(){
for(int i=1;i<10;i++){
System.out.print(i);
try {
sleep(100);
}catch (InterruptedException e){
e.printStackTrace();
}
System.out.println(getName() + ";" + i);
}
}
public static void main(String[]args){
Thread t1 = new ThreadDemo01();
Thread t2 = new ThreadDemo01();
t2.start();//ready
t1.start();
}
}
2.2 匿名内部类
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
public class ThreadDemo02 {
public static void main(String[] args) {
Thread t1 = new Thread(() -> {
});
List<Integer> list = new ArrayList<>();
list.add(1);
list.add(2);
list.add(3);
list.sort(((o1, o2) -> 0));
Integer[] nums = {1, 5, 2, 3};
Arrays.sort(nums, (o1, o2) -> o2 - o1);
Integer[][] a = {{1, 2}, {5, 3}, {3, 1}};
Arrays.sort(a, new Comparator<Integer[]>() {
@Override
public int compare(Integer[] o1, Integer[] o2) {
return o1[1] - o2[1];
}
});
}
}
import java.util.Comparator;
public class ThreadDemo03 {
public static void main(String[] args) {
//匿名内部类
Thread t1 = new Thread(() -> {
for (int i = 1; i < 10; i++) {
System.out.println("A->" + i);
}
});
Thread t2 = new Thread(() -> {
for (int i = 11; i < 100; i++) {
System.out.println("A->" + i);
}
});
t1.start();
t2.start();
}
}
3 Block 阻塞
sleep 延缓
yield 放弃,重开
join 加入
public class BlockDemo01 {
public static void main(String[] args) throws InterruptedException {
Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
for (int i = 0; i < 10; i++) {
try {
Thread.sleep(100);
if (i > 3) Thread.yield();
System.out.println("A->" + i);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
});
Thread t2 = new Thread(new Runnable() {
@Override
public void run() {
for (int i = 0; i < 10; i++) {
try {
Thread.sleep(100);
if (i > 3) Thread.yield();
System.out.println("B->" + i);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
});
t1.start();
t1.join();
t2.start();
}
}
4 守护线程
守护最后一个用户线程结束