0:多线程的通讯
之前的实例都是执行相同的任务,就是Thread 里面的参数都是同一个,这里不同了
/**
多线程的通讯,多个线程执行的任务不同,但是操作的是相同的资源,注意这里执行的任务是不一样的就是Runnable 的对象是不一样的
之前的实例都是执行相同的任务,就是Thread 里面的参数都是同一个,这里不同了
注意这里的锁其实就是Res的对象res,
wait(),notify(),notifyAll() 这些方法都是定义在Object 中的,而且synchronized的参数也就是Object 类型的。
其实就是通过锁来等待,通知和通知所有的
下面的例子相当于是生产一个,消费一个
*/
/**
操作的资源
*/
class Res{
String name;
String sex;
boolean inBox;//是否有生产好的
}
class Input implements Runnable{
Res res;
public Input(){
}
public Input(Res res){
this.res = res;
}
public void run(){
int i = 1;
while(true){
//执行多线程操作的同步的代码,参数是res
synchronized(res){
if(res.inBox){
try{res.wait();}catch(Exception e){}
}
if(i==1){
res.name = "kodulf";
res.sex = "男";
}else{
res.name = "33";
res.sex = "女";
}
res.inBox=true;
i=((i+1)%2);
res.notify();//第一次是空唤醒
}
}
}
}
class Output implements Runnable{
Res res;
public Output(){
}
public Output(Res res){
this.res=res;
}
public void run(){
while(true){
synchronized(res){
if(!res.inBox){
try{res.wait();}catch(Exception e){}
}
System.out.println("名字为"+res.name+" 性别"+res.sex);
res.inBox=false;
res.notify();
}
}
}
}
public class InputAndOutput{
public static void main(String[] args){
Res res = new Res();
//之前的实例都是执行相同的任务,就是Thread 里面的参数都是同一个,这里不同了
//创建输入任务对象
Input input = new Input(res);
//创建输出任务对象
Output output = new Output(res);
//创建输入线程对象
Thread t1 = new Thread(input);
//创建输出线程对象
Thread t2 = new Thread(output);
t1.start();
t2.start();
}
}
优化:
注意这个Res 类中的两个方法是两个同步的方法,这两个的锁都是this
class Res{
public String name;
public String sex;
public boolean inBox;
public synchronized void set(String name,String sex){
if(inBox){
try{this.wait();}catch(Exception e){}//注意这里的wait 前面是this,因为同步函数默认的线程锁就是this,其实也可以不写的。
}
this.name = name;
this.sex = sex;
inBox=true;
this.notify();//注意这里的notify 前面是this,因为同步函数默认的线程锁就是this,其实也可以不写的。
}
public synchronized void out(){
if(!inBox){
try{this.wait();}catch(Exception e){}
}
System.out.println("名字为"+name+" 性别为"+sex);
inBox=false;
this.notify();
}
}
class Input implements Runnable{
Res res;
public Input(Res res){
this.res = res;
}
int i = 1;
public void run(){
while(true){
if(i==1){
res.set("kodulf","man");
}else{
res.set("33","women");
}
i=(i+1)%2;
}
}
}
class Output implements Runnable{
Res res;
public Output(Res res){
this.res = res;
}
public void run(){
while(true){
res.out();
}
}
}
public class InputAndOutputUpdated{
public static void main(String[] args){
Res res = new Res();
Input input = new Input(res);
Output output = new Output(res);
Thread t1 = new Thread(input);
Thread t2 = new Thread(output);
t1.start();
t2.start();
}
}
生产者和消费者:
生产一个消费一个的情况,和上面很类似:
/**需要生产和消费的产品*/
class Product{
String name;
int count;
boolean inBox;
public synchronized void produce(String name){
if(inBox){
try{this.wait();}catch(Exception e){}
}
this.name = name+" "+count;
System.out.println("++++生产了"+this.name);
count++;
inBox=true;
this.notify();
}
public synchronized void consume(){
if(!inBox){
try{this.wait();}catch(Exception e){}
}
System.out.println("----消费了"+name);
inBox=false;
this.notify();
}
}
class Producer implements Runnable{
Product product;
public Producer(Product product){
this.product=product;
}
public void run(){
while(true){
product.produce("笔记本");
}
}
}
class Consumer implements Runnable{
Product product;
public Consumer(Product product){
this.product = product;
}
public void run(){
while(true){
product.consume();
}
}
}
public class ProduceAndConsumeOne{
public static void main(String[] args){
Product product = new Product();
Producer producer = new Producer(product);
Consumer consumer = new Consumer(product);
Thread t1 = new Thread(producer);
Thread t2 = new Thread(consumer);
t1.start();
t2.start();
}
}
多个生产者和多个消费者的情况:
/**需要生产和消费的产品*/
class Product{
String name;
int count;
boolean inBox;
public synchronized void produce(String name){
//为了防止生产一个消费多次,或者生产一次,消费多次,将if 变成while,但是这样有会出现死锁的情况,
//死锁的原因是但线程环形的是本方线程的时候,导致所有的线程全部等待,这个时候将notify 改为notifyAll();
while(inBox){
try{this.wait();}catch(Exception e){}
}
this.name = name+" "+count;
System.out.println(Thread.currentThread().getName()+"++++生产了"+this.name);
count++;
inBox=true;
//死锁的原因是但线程环形的是本方线程的时候,导致所有的线程全部等待,这个时候将notify 改为notifyAll();
this.notifyAll();
}
public synchronized void consume(){
while(!inBox){
try{this.wait();}catch(Exception e){}
}
System.out.println(Thread.currentThread().getName()+"----消费了"+name);
inBox=false;
this.notifyAll();
}
}
class Producer implements Runnable{
Product product;
public Producer(Product product){
this.product=product;
}
public void run(){
while(true){
product.produce("笔记本");
}
}
}
class Consumer implements Runnable{
Product product;
public Consumer(Product product){
this.product = product;
}
public void run(){
while(true){
product.consume();
}
}
}
public class ProduceAndConsumeMore{
public static void main(String[] args){
Product product = new Product();
Producer producer = new Producer(product);
Consumer consumer = new Consumer(product);
Thread p1 = new Thread(producer);
Thread p2 = new Thread(producer);
Thread p3 = new Thread(producer);
Thread p4 = new Thread(producer);
Thread c1 = new Thread(consumer);
Thread c2 = new Thread(consumer);
Thread c3 = new Thread(consumer);
Thread c4 = new Thread(consumer);
p1.start();
p2.start();
p3.start();
p4.start();
c1.start();
c2.start();
c3.start();
c4.start();
}
}