Java线程入门第二篇

Java线程通信方法

0、(why)每个线程都有自己的栈空间,我们要线程之间进行交流,合作共赢。

1、synchronized和volatile关键字

  a)  看下面的synchronized关键字

  b)  看下面的volatile关键字

2、等待/通知机制:一个线程A调用对象的wait()方法,另一个线程调用线程B的notity()或者的notifyall()方法.

  a)  顺序打印奇数偶数

public class ThreadPrintDemo2 {

    public static void main(String[] args) {
        final ThreadPrintDemo2 demo2 = new ThreadPrintDemo2();
        //java8新特性
        Thread t1 = new Thread(demo2 :: print1);
        Thread t2 = new Thread(demo2 :: print2);
        t1.start();
        t2.start();
    }
    public   synchronized void print2() {

        for (int i = 1; i <= 100; i +=2) {
            System.out.println(i);
            this.notify();  //通知等待中的进程
            try {
                this.wait();    //线程进入等待
                Thread.sleep(100);// 防止打印速度过快导致混乱
            } catch (InterruptedException e) {
                // NO
            }
        }
    }
    public synchronized void print1() {
        for (int i = 0; i <= 100; i += 2) {
            System.out.println(i);
            this.notify();  //通知等待中的进程
            try {
                this.wait();    //线程进入等待
                Thread.sleep(100);// 防止打印速度过快导致混乱
            } catch (InterruptedException e) {
                // NO
            }
        }
    }
}

  b) 打印连续句子

public class ThreadPrintDemo2 {
    private char[] arr = new char[]{'a', ' ', 'b', 'c', 'd', 'e'};

    public static void main(String[] args) {
        final ThreadPrintDemo2 demo2 = new ThreadPrintDemo2();
        Thread t1 = new Thread(demo2::print1);
        Thread t2 = new Thread(demo2::print2);
        t1.start();
        t2.start();
    }
    public synchronized void print2() {

        for (int i = 1; i < arr.length; i +=2) {
            System.out.print(arr[i]);
            this.notify();
            try {
                this.wait();
                Thread.sleep(100);// 防止打印速度过快导致混乱
            } catch (InterruptedException e) {
                // NO
            }
        }
    }
    public synchronized void print1() {
        for (int i = 0; i < arr.length; i +=2) {
            System.out.print(arr[i]);
            this.notify();
            try {
                this.wait();
                Thread.sleep(100);// 防止打印速度过快导致混乱
            } catch (InterruptedException e) {
                // NO
            }
        }
    }
}

结果:a bcde

3、管道输入输出流:pipedreader和pipedwriter 面向字符

          Pipedoutputstream和pipedinputstream面向字节

4、使用thread.join()方法:利用线程等待特性

5、使用ThreadLocal线程变量

Synchronize关键字(重量级锁)

1、基础:Java中每一个对象都可以作为锁

2、手段:确保多线程在同一时刻,只有一个线程处于方法或同步块中

3、实现原理:JVM基于进入和退出monitor对象来实现方法同步和代码块同步

4、锁优化:jdk1.6之后优化了锁,加入了偏向锁和轻量级锁,锁可以升级但不能降级

5、使用场景:方法  代码块

Lock接口

1、他是一个接口

2、使用的时候需要显式获取锁

3、使用方法:

Lock lock = new ReentrantLock();
lock.lock();
try {
    
}finally {
    lock.unlock();
}

4、Synchronized经常与Lock放在一起比较

Volatile

1、是什么:(what)是轻且量级的synchronize,保证共享变量的可见性(一个线程修改一个共享变量的时候,另一个线程会知道),并他不会引起线程的上下文切换和调度。

2、使用场景:修饰变量

3、优化:在1.7中将共享变量追加了60个字节,变成64个字节。因为(硬件)目前流行的cpu的高速缓存行是64个字节,当队列的头尾节点不足64字节时候,处理器会将他们读到同一个缓存行,并且锁定缓存行。这样会影响队列出队入队效率。

ThreadLoacl类(线程变量)

为每个线程变量创建一个该变量的副本,避免并发访问的线程安全问题,保证线程安全

原文地址:https://www.cnblogs.com/chenzhuantou/p/10171479.html