第9章 多线程
Posted 叶十一少
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了第9章 多线程相关的知识,希望对你有一定的参考价值。
1、
/* 需求:设计4个线程对象,两个线程执行减操作,两个线程执行加操作。 类似于多个生产者和多个消费者的例子 */ import java.util.concurrent.locks.Condition; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; class Resource { private int num; private boolean flag = false; private Lock lock = new ReentrantLock(); private Condition condition_add = lock.newCondition(); private Condition condition_sub = lock.newCondition(); public void add() throws InterruptedException { lock.lock(); try { while(flag) { condition_add.await(); } this.num++; System.out.println(Thread.currentThread().getName() + "...执行加操作之后... num = " + num); flag = true; condition_sub.signal(); } finally { lock.unlock(); } } public void subtract() throws InterruptedException { lock.lock(); try { while(!flag) { condition_sub.await(); } this.num--; System.out.println(Thread.currentThread().getName() + "........执行减操作之后... num = " + num); flag = false; condition_add.signal(); } finally { lock.unlock(); } } } class Addition implements Runnable { private Resource res; Addition(Resource res) { this.res = res; } public void run() { while(true) { try { res.add(); } catch (InterruptedException e) { e.printStackTrace(); } } } } class Subtraction implements Runnable { private Resource res; Subtraction(Resource res) { this.res = res; } public void run() { while(true) { try { res.subtract(); } catch (InterruptedException e) { e.printStackTrace(); } } } } public class Practise1 { public static void main(String[] args) { Resource res = new Resource(); Addition add = new Addition(res); Subtraction sub = new Subtraction(res); Thread t1 = new Thread(add, "加操作——线程1"); Thread t2 = new Thread(add, "加操作——线程2"); Thread t3 = new Thread(sub, "减操作——线程1"); Thread t4 = new Thread(sub, "减操作——线程2"); t1.start(); t2.start(); t3.start(); t4.start(); } }
运行部分示意图:
2、
import java.util.concurrent.locks.Condition; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; class Computer { private String name; private int num = 1; private boolean flag = false; private static int count = 0; private Lock lock = new ReentrantLock(); //private Condition condition_producer = lock.newCondition(); //private Condition condition_carrier = lock.newCondition(); private Condition condition = lock.newCondition(); public void setComputer(String name) throws InterruptedException { lock.lock(); try { while(flag) { condition.await(); } this.name = name + "-型号:" + num++; System.out.println(Thread.currentThread().getName() + "生产电脑——>" + this.name + ",共生产了" + count++ + "台电脑"); flag = true; condition.signal(); } finally { lock.unlock(); } } public void getComputer() throws InterruptedException { lock.lock(); try { while(!flag) { condition.await(); } System.out.println(Thread.currentThread().getName() + "搬运电脑——>" + this.name); flag = false; condition.signal(); } finally { lock.unlock(); } } } class Producer implements Runnable { private Computer com; Producer(Computer com) { this.com = com; } public void run() { while(true) { try { com.setComputer("苹果"); } catch (InterruptedException e) { e.printStackTrace(); } } } } class Carrier implements Runnable { private Computer com; Carrier(Computer com) { this.com = com; } public void run() { while(true) { try { com.getComputer(); } catch (InterruptedException e) { e.printStackTrace(); } } } } public class Practise2 { public static void main(String[] args) { Computer computer = new Computer(); Producer producer = new Producer(computer); Carrier carrier = new Carrier(computer); Thread t1 = new Thread(producer, "生产1号线"); Thread t2 = new Thread(carrier, "1号搬运工人"); t1.start(); t2.start(); } }
运行示意部分截图:
以上是关于第9章 多线程的主要内容,如果未能解决你的问题,请参考以下文章