多线程

多线程

Thread 和 Runnable

1.线程开启不一定立即执行,由CPU调度执行

image-20240108225246624

2.Callable 接口

image-20240114143421441

3.静态代理模式

image-20240114144933536

public class StaticProxy {
    public static void main(String[] args) {
        weddingcompany ta=new weddingcompany(new You());
        ta.HappyMarry();
    }
    
}
interface Marry{

    void HappyMarry ();
}

class You implements Marry {
    @Override
    public void HappyMarry() {
        System.out.println("秦老师要结婚了,超开心");

    }
}
class weddingcompany implements Marry{
    private Marry target;
    public weddingcompany(Marry target){
        this.target=target;
    }
        @Override
        public void HappyMarry () {
            before();
            this.target.HappyMarry();
            after();
        }
        private void after(){
            System.out.println("结婚之后,收尾款");
        }
        private void before(){
            System.out.println("结婚之前,布置现场");
        }
    }

4.Lamda表达式

image-20240114145246059

image-20240114145304646

image-20240114152130712

5.线程状态和方法

image-20240114200508662

image-20240114200527086

1.停止线程

image-20240114200941660

2.线程休眠

image-20240114201626277

3.线程礼让

image-20240114214224750

public class TestYield {
    public static void main(String[] args) {
       MyYield myYield=new MyYield();
       new Thread(myYield,"a").start();
       new Thread(myYield,"b").start();
    }
}

class MyYield implements Runnable{
    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName()+"线程开始");
        Thread.yield();//礼让
        System.out.println(Thread.currentThread().getName()+"线程停止");
    }
}

4.Join

image-20240114214653043

5.线程优先级

image-20240114215712195

6.守护线程

image-20240114220320684

6.线程同步

采用synchiozed方法

7.死锁

image-20240115103133419

public class DeadLock {
    public static void main(String[] args) {
        Makeup makeup = new Makeup(0, "1");
        Makeup makeup2 = new Makeup(1, "2");
        makeup.start();
        makeup2.start();
    }
}
//口红
class Lipstick{

}
//镜子
class Mirrot{

}

class Makeup extends Thread{
    static Lipstick lipstick=new Lipstick();
    static Mirrot mirrot=new Mirrot();

    int choice;

    String girlName;

    Makeup(int choice,String girlName){
        this.choice=choice;
        this.girlName=girlName;
    }
    @Override
    public void run() {
        try {
            makeup();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }

    //化妆,互相持有对方的锁
    private void makeup() throws InterruptedException {
        if(choice==0){
            synchronized (lipstick){
                System.out.println(this.girlName+"获得口红的锁");
                Thread.sleep(1000);


            } synchronized (mirrot){
                System.out.println(this.girlName+"获得镜子的锁");
            }
        }else{
            synchronized (mirrot){
                System.out.println(this.girlName+"获得镜子的锁");
                Thread.sleep(2000);


            } synchronized (lipstick){
                System.out.println(this.girlName+"获得口红的锁");
            }
        }

    }
}

image-20240115104500916

8.Lock

image-20240115105051368

image-20240115105124914

9.线程通信

image-20240115110040131

public class TestPc {
    public static void main(String[] args) {
        Container container=new Container();
        new Producer(container).start();
        new Consumer(container).start();
    }
}

class Producer extends Thread{
    Container container;
    public Producer(Container container){
        this.container=container;
    }

    @Override
    public void run() {
        for (int i = 1; i < 100; i++) {
            System.out.println("生产了"+i+"鸡");
            try {
                container.push(new Chicken(i));
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }


        }
    }
}

class Consumer extends Thread{
    Container container;
    public Consumer(Container container){
        this.container=container;
    }

    @Override
    public void run() {
        for (int i = 1; i < 100; i++) {
            try {
                System.out.println("消费了-->"+ container.pop().id+"鸡");
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }

        }
    }
}

class  Chicken{
    int id;
    public Chicken(int id){
        this.id=id;
    }
}


//缓冲区
class Container{
    //需要一个容器大小
    Chicken[] chickens=new Chicken[10];

    int count=0;

    public synchronized void push(Chicken chicken) throws InterruptedException {
        if(count==chickens.length){
            //等待
            try{
                wait();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }

        }
        chickens[count++]=chicken;

        this.notify();

    }

    public synchronized Chicken pop() throws InterruptedException {
        if(count==0){
            //等待生产者生产,消费者等待
            try{
                wait();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }

        }

        Chicken chicken=chickens[--count];
        this.notify();
        return chicken;
    }
}

image-20240115112905070

image-20240115112931450

10.线程池

image-20240115113054120

image-20240115113636498

public class TestPool {
    public static void main(String[] args) {
        //1.创建服务,创建线程池
        ExecutorService service= Executors.newFixedThreadPool(10);

        service.execute(new MyThread());
        service.execute(new MyThread());

        service.shutdown();
    }
}

class MyThread implements Runnable{
    @Override
    public void run() {
        for (int i = 0; i < 10; i++) {
            System.out.println(Thread.currentThread().getName()+"  "+i);
        }
    }
}
posted @ 2024-01-15 11:45  wnbzw  阅读(5)  评论(0)    收藏  举报