线程与进程

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 守护线程

守护最后一个用户线程结束

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值