多线程基础

2019-10-27  本文已影响0人  木山手札

JVM中的线程

JVM线程模型

线程的创建方式

这三种创建线程的差异在哪里

wait()

synchronized(obj){
  while(条件不满足){
    obj.wait();
  }
}
public class ProductConsumer {
    private static final Integer capacity = 5;

    private static final LinkedBlockingQueue queue = new LinkedBlockingQueue(capacity);


    public static void main(String[] args) throws InterruptedException, IOException {
        Producer producer = new Producer();
        Consumer consumer = new Consumer();

        Thread p1 = new Thread(producer);
        Thread c1 = new Thread(consumer);
        Thread c2 = new Thread(consumer);

        p1.start();
        c1.start();
        c2.start();

        p1.join();
        c1.join();
        c2.join();

        System.out.println("will read in wait");
        System.in.read();
    }

    private static class Producer implements Runnable {


        @Override
        public void run() {
            try {
                while (true) {
                    synchronized (queue) {
                        while (queue.size() == capacity) {
                            System.out.println("producer wait");
                            queue.wait();
                        }
                        long product = System.currentTimeMillis();
                        queue.add(product);
                        System.out.println("product=" + product);
                        queue.notifyAll();
                    }
                    TimeUnit.MILLISECONDS.sleep(100);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    private static class Consumer implements Runnable {

        @Override
        public void run() {
            try {
                while (true) {
                    synchronized (queue) {
                        while (queue.size() == 0) {
                            System.out.println("consumer wait");
                            queue.wait();
                        }
                        System.out.println("consumer=" + queue.take());
                        queue.notifyAll();
                    }
                    TimeUnit.SECONDS.sleep(2);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}

notify()/notifyAll()

join()

yield()

线程中断

 public static boolean interrupted() {
        // 获取当前线程,而不是调用interrupted()的实例对象线程中的中断标志
        return currentThread().isInterrupted(true);
    }
     Thread t1 = new Thread(()->{
            for(;;);
        });

        t1.start();
        t1.interrupt();

        Thread.currentThread().interrupt();
        
        System.out.println(t1.isInterrupted());
        System.out.println(t1.interrupted());
        System.out.println(Thread.interrupted());
        System.out.println(t1.isInterrupted());

        t1.join();

死锁

如何实现一个死锁程序

ThreadLocal

内部结构ThreadLoacl、TheradLoalMap
Thread中的两个变量threadLoacls、inheritableThreadLocals
ThreadLoacl中的set、get、rmove方法实现
子线程、父线程共享数据

上一篇下一篇

猜你喜欢

热点阅读