1、线程间通信-等待唤醒机制
Class Res
{
String name;
String sex;
boolean flag=false;
}
class Input implements Runnable
{
private Res r;
boolean b=true;
Input(Res r)
{
this.r=r;
}
public void run()
{
while(true)
{
synchronized(r)
{
if(r.flag)
try{r.wait();}catch(Exception e){}
if(b)
{
r.name="mike";
r.sex="man";
b=false;
}
else
{
r.name="丽丽";
r.sex="女女女女女女";
b=true;
}
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();
}
}
}
}
class InputOutputDemo
{
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();
}
}
都使用在同步中,因为要对持有监视器()
2、线程间通信-代码优化
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;
boolean b=true;
Input(Res r)
{
this.r=r;
}
public void run()
{
while(true)
{
if(b)
{
r.set("mike","man");
b=false;
}
else
{
r.set("丽丽","女女女女女女");
b=true;
}
}
}
}
class Output implements Runnable
{
private Res r;
Output(Res r)
{
this.r=r;
}
public void run()
{
while(true)
{
r.out();
}
}
}
class InputOutputDemo
{
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();
*/
new Thread(new Input(r)).start();
new Thread(new Output(r)).start();
}
}
3、线程间通信-生产者消费者
class Resource
{
private String name;
private int count=1;
private boolean flag=false;
public synchronized void set(String name)
{
while(flag)
try{this.wait();}catch(Exception e){}
this.name=name+"---"+count++;
System.out.println(Thread.currentThread().getName()+"...生产者..."+this.name);
flag=true;
this.notifyAll();
}
public synchronized void out()
{
while(!flag)
try{this.wait();}catch(Exception e){}
System.out.println(Thread.currentThread().getName()+"...消费者......"+this.name);
flag=false;
this.notifyAll();
}
}
class Producer implements Runnable//生产者
{
private Resource r;
Producer(Resource r)
{
this.r=r;
}
public void run()
{
while(true)
r.set("商品");
}
}
class Consumer implements Runnable//消费者
{
private Resource r;
Consumer(Resource r)
{
this.r=r;
}
public void run()
{
while(true)
r.out();
}
}
class ProducerConsumerDemo
{
public static void main(String[] args)
{
Resource r=new Resource();
new Thread(new Producer(r)).start();
new Thread(new Producer(r)).start();
new Thread(new Consumer(r)).start();
new Thread(new Consumer(r)).start();
}
}
4、生产者消费者JDK5.0升级版
import java.util.concurrent.locks.*;
/*
JDK1.5中提供了多线程升级解决方案。
将同步Synchronized替换成现实lock操作。
将Object中的wait,notify notifyAll,替换成Condition对象
该对象可以Lock锁 进行获取。
该示例中,实现了本方只唤醒对方的操作。
*/
class Resource
{
private String name;
private int count=1;
private boolean flag=false;
private final Lock lock=new ReentrantLock();//创建一个锁
//private Condition condition=lock.newCondition();//创建一个Condition实例
private Condition condition_Pro=lock.newCondition();
private Condition condition_Con=lock.newCondition();//一个锁上多个相关的condition
public void set(String name)throws InterruptedException
{
lock.lock();
try
{
while(flag)
//try{this.wait();}catch(Exception e){}
condition_Pro.await();
this.name=name+"---"+count++;
System.out.println(Thread.currentThread().getName()+"...生产者..."+this.name);
flag=true;
//this.notifyAll();
condition_Con.signal();
}
finally
{
lock.unlock();
}
}
public void out()throws InterruptedException
{
lock.lock();
try
{
while(!flag)
//try{this.wait();}catch(Exception e){}
condition_Con.await();
System.out.println(Thread.currentThread().getName()+"...消费者......"+this.name);
flag=false;
//this.notifyAll();
condition_Pro.signal();
}
finally
{
lock.unlock();
}
}
}
class Producer implements Runnable//生产者
{
private Resource r;
Producer(Resource r)
{
this.r=r;
}
public void run()
{
while(true)
try
{
r.set("商品");
}
catch(InterruptedException e)
{
}
}
}
class Consumer implements Runnable//消费者
{
private Resource r;
Consumer(Resource r)
{
this.r=r;
}
public void run()
{
while(true)
try
{
r.out();
}
catch(InterruptedException e)
{
}
}
}
class ProducerConsumerDemo2
{
public static void main(String[] args)
{
Resource r=new Resource();
new Thread(new Producer(r)).start();
new Thread(new Producer(r)).start();
new Thread(new Consumer(r)).start();
new Thread(new Consumer(r)).start();
}
}
5、停止线程
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()+"...test");
}
}
}
class StopThreadDemo
{
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++==60)
{
//st.changeFlag();
t1.interrupt();
t2.interrupt();
break;
}
System.out.println(Thread.currentThread().getName()+"......."+num);
}
System.out.println("over");
}
}
6、守护线程
t1.setDaemon(true);
t2.setDaemon(true);
7、Join方法
class Demo implements Runnable
{
public void run()
{
for(int x=0;x<70;x++)
System.out.println(Thread.currentThread().getName()+"......"+x);
}
}
class JoinDemo
{
public static void main(String[] args)throws InterruptedException
{
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<80;x++)
{
System.out.println("main..."+x);
}
System.out.println("over");
}
}
8、优先级&yield方法
class ThreadTest
{
public static void main(String[] args)
{
new Thread()
{
public void run()
{
for(int x=0;x<100;x++)
System.out.println(Thread.currentThread().getName()+"..."+x);
}
}.start();
Runnable r=new Runnable()
{
public void run()
{
for(int x=0;x<100;x++)
System.out.println(Thread.currentThread().getName()+"..."+x);
}
};
r.run();
for(int x=0;x<100;x++)
System.out.println(Thread.currentThread().getName()+"..."+x);
for(int x=0;x<100;x++)
System.out.println(Thread.currentThread().getName()+"..."+x);
}
}