java中获取多线程的方式有四种
实现runable
callable
高内聚、低耦合
实现thead
在多线程的判断的时候超过3个线程的情况下则为多
判断的条件则要用while来用来判断择不能用if用来判断
用while的条件的含义是、每次都对其进行判断而不是对其只判断一次
因此上面的条件中可以将if的判断改为while的判断就可以了
上面的判断内容只能是两个线程进行的内容
然后使用while来判断的时候可以存在多个线程(3个或者三个以上的情况)
实现runable
callable
线程操作资源类
实现thead
---runable和callable的区别
---演示案例callableeg:
---线程的同步和通信
----Object中的常用的方法
---通知唤醒机制的案例
---多线程的理解和虚假唤醒
如果存在多个的情况下、可能会出现线程的虚假唤醒
如果出现四次的情况下呢、两次增加、两次减少、就可能会出现线程的虚假唤醒的情况在多线程的判断的时候超过3个线程的情况下则为多
判断的条件则要用while来用来判断择不能用if用来判断
用while的条件的含义是、每次都对其进行判断而不是对其只判断一次
因此上面的条件中可以将if的判断改为while的判断就可以了
上面的判断内容只能是两个线程进行的内容
然后使用while来判断的时候可以存在多个线程(3个或者三个以上的情况)
api中的解释
使用lock的情况下
---code如下
package com.atguigu.thread1018; import java.util.concurrent.locks.Condition; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; class ShareData { private int number = 0; private Lock lock = new ReentrantLock(); private Condition condition = lock.newCondition(); public void increment() throws InterruptedException { lock.lock(); try { while(number != 0) { condition.await();//this.wait(); } ++number; System.out.println(Thread.currentThread().getName()+"\t"+number); condition.signalAll();//this.notifyAll(); } catch (Exception e) { e.printStackTrace(); } finally { lock.unlock(); } } public void decrement() throws InterruptedException { lock.lock(); try { while(number == 0) { condition.await();//this.wait(); } //2 干活 --number; System.out.println(Thread.currentThread().getName()+"\t"+number); //3 通知 condition.signalAll();//this.notifyAll(); } catch (Exception e) { e.printStackTrace(); } finally { lock.unlock(); } } /*public synchronized void increment() throws InterruptedException { //1 判断 //if(number != 0) while(number != 0) { this.wait();// A......C...... } //2 干活 ++number; System.out.println(Thread.currentThread().getName()+"\t"+number); //3 通知 this.notifyAll(); } public synchronized void decrement() throws InterruptedException { //1 判断 //if(number == 0) while(number == 0) { this.wait(); } //2 干活 --number; System.out.println(Thread.currentThread().getName()+"\t"+number); //3 通知 this.notifyAll(); } */ } /** * * @Description: * 现在两个线程, * 可以操作初始值为零的一个变量, * 实现一个线程对该变量加1,一个线程对该变量减1, * 交替,来10轮,变量初始值为零。 * @date 2018年3月15日 * 1 多线程编写套路------上 * 1.1 线程 操作(实例方法) 资源类 * 1.2 高内聚 低耦合 * * 2 多线程编写套路------下 * 2.1 判断 * 2.2 干活 * 2.3 通知 */ public class NotifyWaitDemo { public static void main(String[] args) { ShareData sd = new ShareData(); new Thread(() -> { for (int i = 1; i <=10; i++) { try { Thread.sleep(200); sd.increment(); } catch (InterruptedException e) { e.printStackTrace(); } } }, "AA").start(); new Thread(() -> { for (int i = 1; i <=10; i++) { try { Thread.sleep(300); sd.decrement(); } catch (InterruptedException e) { e.printStackTrace(); } } }, "BB").start(); new Thread(() -> { for (int i = 1; i <=10; i++) { try { Thread.sleep(400); sd.increment(); } catch (InterruptedException e) { e.printStackTrace(); } } }, "CC").start(); new Thread(() -> { for (int i = 1; i <=10; i++) { try { Thread.sleep(500); sd.decrement(); } catch (InterruptedException e) { e.printStackTrace(); } } }, "DD").start(); } }