多线程间通信

线程间通讯:其实就是多个线程在操作同一个资源,但是操作的动作不同。

等待唤醒机制:

public static void main(String[] args) {
		Res r = new Res();
		
		Input in = new Input(r);
		Output out = new Output(r);
		
		Thread t1 = new Thread(in);
		Thread t2 = new Thread(out);
		
		t1.start();
		t2.start();
	}
}
class Res{
	 String name;
	 String sex;
	 boolean flag = false;
}
class Input implements Runnable{
	private Res r;
	Input(Res r)
	{
		this.r = r;
	}
	public void run() {
		int x = 0;
		while(true) {
			synchronized(r) {
				if(r.flag) {
					try {r.wait();}catch(Exception e) {}
				}
				if(x==0) 
				{
					r.name = "mike";
					r.sex = "man";
				}
				else 
				{
					r.name="丽丽";
					r.sex = "女女女";						
				}
				x = (x+1)%2;
				r.flag=true;
				r.notify();
			}
		}
	}
}
class Output implements Runnable{
		private Res r;
		Output(Res r){
			this.r = r;
		}
	
	public void run() {		
		while(true) {
			synchronized (r){
				if(!r.flag) {
					try {r.wait();}catch(Exception e) {}
					System.out.println(r.name+"...."+r.sex);
					r.flag=false;
					r.notify();
				}
			}
		}
	}	
}

简化:

public static void main(String[] args) {
		Res r = new Res();
		new Thread(new Input(r)).start();
		new Thread(new Output(r)).start();
	}
}
class Res{
	 private String name;
	 private String sex;
	 private boolean flag = false;
	 public synchronized void set(String name,String sex) {
		 if(flag)
			 try {this.wait();}catch(Exception e) {}
		 this.name=name;
		 this.sex=sex;
		 flag=true;
		 this.notify();
	 }
	 public synchronized void out() {
		 if(!flag)
			 try {this.wait();}catch(Exception e) {}
		 System.out.println(name+"..."+sex);
		 flag=false;
		 this.notify();
	 }
}
class Input implements Runnable{
	private Res r;
	Input(Res r)
	{
		this.r = r;
	}
	public void run() {
		int x = 0;
		while(true) {		
				if(x==0) 
				{
					r.set("mike", "man");
				}
				else 
				{
					r.set("lili", "女女");					
				}
				x = (x+1)%2;
			}
		}
	}
class Output implements Runnable{
		private Res r;
		Output(Res r){
			this.r = r;
		}
	
	public void run() {		
		while(true) {
			r.out();
		}
	}	
}

生产者消费者例子:

public static void main(String [] args) {
		Resource r = new Resource();
		Producer pro = new Producer(r);	
		Consumer con = new Consumer(r);
		Thread t1 = new Thread(pro);
		Thread t2 = new Thread(pro);
		Thread t3 = new Thread(con);
		Thread t4 = new Thread(con);
		t1.start();
		t2.start();
		t3.start();
		t4.start();
	}
}
class Resource{
	private String name;
	private int count=1;
	private boolean flag=false;
	public synchronized void set(String name) {
		while(flag)//为什么不用if,因为if判断只执行一次会造成多生产少销售,或者相反。while会造成全部等待
			try {wait();}
		catch(Exception e) {}
		this.name=name+"--"+count++;
		System.out.println(Thread.currentThread().getName()+"...生产者..."+this.name);
		flag=true;
		this.notifyAll();//为了防止出现全部等待notify改成notifyAll。
	}
	public synchronized void out() {
		while(!flag)
		try {wait();}
		catch(Exception e) {}
		System.out.println(Thread.currentThread().getName()+"...消费者..."+this.name);
		flag=false;
		this.notifyAll();
	}
}
class Producer implements Runnable{
	private Resource res;
	Producer(Resource res){
		this.res=res;
	}
	public void run() {
		while(true) {
			res.set("+商品+");
		}
	}
}
class Consumer implements Runnable{
	private Resource res;
	Consumer(Resource res){
		this.res=res;
	}
	public void run() {
		while(true) {
			res.out();
		}
	}
}

 

唤醒对方线程特性:

package com.test1;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class Prodect {
	public static void main(String [] args) {
		Resource r = new Resource();
		Producer pro = new Producer(r);	
		Consumer con = new Consumer(r);
		Thread t1 = new Thread(pro);
		Thread t2 = new Thread(pro);
		Thread t3 = new Thread(con);
		Thread t4 = new Thread(con);
		t1.start();
		t2.start();
		t3.start();
		t4.start();
	}
}
class Resource{
	private String name;
	private int count=1;
	private boolean flag=false;
	private Lock lock = new ReentrantLock();
	private Condition condition =lock.newCondition();
	private Condition condition_pro =lock.newCondition();
	private Condition condition_con =lock.newCondition();
	public void set(String name) throws InterruptedException {
		lock.lock();
		try {
		while(flag)	
			condition_pro.await();
		this.name=name+"--"+count++;
		System.out.println(Thread.currentThread().getName()+"...生产者..."+this.name);
		flag=true;
		condition_con.signal();
		}		
		finally{
			lock.unlock();
		}
	}
	public void out()throws InterruptedException {
		lock.lock();
		try {
		while(!flag)
		   condition_con.await();
		System.out.println(Thread.currentThread().getName()+"...消费者"+this.name);
		flag=false;
		  condition_pro.signal();
		}
		finally {
			lock.unlock();
		}
	}
}
class Producer implements Runnable{
	private Resource res;
	Producer(Resource res){
		this.res=res;
	}
	public void run() {
		while(true) {
			try {
				res.set("+商品+");
			}
			catch(InterruptedException e){}
			
		}
	}
}
class Consumer implements Runnable{
	private Resource res;
	Consumer(Resource res){
		this.res=res;
	}
	public void run() {
		while(true) {
			try {
				res.out();
			}
			catch(InterruptedException e){}
			
		}
	}
}

停止线程:

	public static void main(String[] args) {
			StopThread st = new StopThread();
			Thread t1 = new Thread(st);
			Thread t2 = new Thread(st);
			t1.start();
			t2.start();
			int num = 0;
			while(true){
				if(num++ == 20) {
					st.changeFlag();
					break;
				}
				System.out.println(Thread.currentThread().getName()+"....main  "+num);
			}
		}
}
class StopThread implements Runnable{
	private boolean flag = true;
	public void run() {
		while(flag) {
			System.out.println(Thread.currentThread().getName()+"...run");
		}	
	}
	public void changeFlag() {
		flag = false;
	}
}

守护线程:

public static void main(String[] args) {
			StopThread st = new StopThread();
			Thread t1 = new Thread(st);
			Thread t2 = new Thread(st);
//在线程前调用
			t1.setDaemon(true);
			t2.setDaemon(true);
			t1.start();
			t2.start();
			int num = 0;
			while(true){
				if(num++ == 20) {
					//t1.interrupt();
					//t2.interrupt();
					//st.changeFlag();
					break;
				}
				System.out.println(Thread.currentThread().getName()+".... "+num);
			}
			System.out.println("over");
		}
}
class StopThread implements Runnable{
	private boolean flag = true;
	public synchronized void run() {
		while(flag) {
			try {
				wait();
			}
			catch(InterruptedException e) {
				System.out.println(Thread.currentThread().getName()+"..Exception");
				flag = false;
			}
			System.out.println(Thread.currentThread().getName()+"...run");
		}	
	}
	public void changeFlag() {
		flag = false;
	}
}

Join方法:

Thread.yield();临时释放。

public static void main(String[] args)throws Exception {	
		Demo d = new Demo();
		Thread t1 = new Thread(d);
		Thread t2 = new Thread(d);
		t1.start();
		t1.setPriority(Thread.MAX_PRIORITY);
		//t1.join();
		t2.start();
		for(int x = 0;x<70;x++) {
			System.out.println("main..."+x);
		}
		System.out.println("over");
	}

}
class Demo implements Runnable{
	public void run() {
		for(int x = 0;x < 60;x++) {
			System.out.println(Thread.currentThread().getName()+"..."+x);
		}
	}
}

如何同时运行线程的方式:

public static void main(String[] args)throws Exception {
		new Thread() { 
			public void run() {
				for(int x= 0;x<100;x++) {
					System.out.println(Thread.currentThread().getName()+"..."+x);
					}
			}	
		}.start();
		for(int x= 0;x<100;x++) {
			System.out.println(Thread.currentThread().getName()+"."+x);
		}
		Runnable r= new Runnable() {
			public void run(){
			for(int x= 0;x<100;x++) {
				System.out.println(Thread.currentThread().getName()+"......"+x);
				}
			}
		};
		new Thread(r).start();
	}
}

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值