一、生产者消费者问题
(1)生产者负责生产,消费者负责消费
(2)生产者生产的产品,消费者消费的是产品
(3)生产者有生产任务,消费者有消费任务
(3)生产和消费可以同时进行
需要描述产品
定义生产者的任务
定义消费者的任务
//需要描述产品
class Product
{
private String name;
private int count;
private boolean flag;
//生产功能
public synchronized void produce(String name)
{
while(flag)
{
try{this.wait();}catch(InterruptedException e){e.printStackTrace();}
}
this.name=name+"..."+count;
count++;
System.out.println(Thread.currentThread().getName()+"...生产了..."+this.name);
flag=true;
this.notifyAll();
}
//消费功能
public synchronized void consume()
{
while(!flag)
try{this.wait();}catch(InterruptedException e){e.printStackTrace();}
System.out.println(Thread.currentThread().getName()+"...消费了..."+this.name);
flag=false;
this.notifyAll();
}
}
//定义生产者的任务
class ProduceTask implements Runnable
{
private Product pro;
ProduceTask(Product pro)
{
this.pro=pro;
}
public void run()
{
while(true)
{
pro.produce("笔记本");
}
}
}
//定义消费者的任务
class ConsumeTask implements Runnable
{
private Product pro;
ConsumeTask(Product pro)
{
this.pro=pro;
}
public void run()
{
while(true)
{
pro.consume();
}
}
}
class Demo1
{
public static void main(String[] args)
{
Product pro = new Product();
//创建生产任务对象
ProduceTask proTask=new ProduceTask(pro);
//创建消费任务对象
ConsumeTask conTask=new ConsumeTask(pro);
//生产线程
Thread t1=new Thread(proTask);
Thread t2=new Thread(proTask);
//消费线程
Thread t3=new Thread(conTask);
Thread t4=new Thread(conTask);
t1.start();
t2.start();
t3.start();
t4.start();
}
}
结果
两个生产线程,两个消费线程,出现了生产一次,消费两次,或者生产两次,消费一次的现象
Thread-0…生产了…笔记本…91130
Thread-2…消费了…笔记本…91130
Thread-3…消费了…笔记本…91130
Thread-0…生产了…笔记本…296
Thread-1…生产了…笔记本…297
Thread-2…消费了…笔记本…297
解决方式:将if判断语句改为while语句
但是出现了死锁现象
把if改成while,出现了死锁,原因是线程唤醒的是使用相同锁的等待线程的任意一个,
如果唤醒的是本方线程,导致所有线程全部等待,所以死锁。
下面再把notify()改成notifAll(),问题解决了,但是不该唤醒的线程也被唤醒了,程序性能降低了。解决这个问题需要使用jdk1.5提供的新的API。解决的代码如下:
//需要描述产品
class Product
{
private String name;
private int count;
private boolean flag;
//生产功能
public synchronized void produce(String name)
{
while(flag)
{
try{this.wait();}catch(InterruptedException e){e.printStackTrace();}
}
this.name=name+"..."+count;
count++;
System.out.println(Thread.currentThread().getName()+"...生产了..."+this.name);
flag=true;
this.notifyAll();
}
//消费功能
public synchronized void consume()
{
while(!flag)
try{this.wait();}catch(InterruptedException e){e.printStackTrace();}
System.out.println(Thread.currentThread().getName()+"...消费了..."+this.name);
flag=false;
this.notifyAll();
}
}
//定义生产者的任务
class ProduceTask implements Runnable
{
private Product pro;
ProduceTask(Product pro)
{
this.pro=pro;
}
public void run()
{
while(true)
{
pro.produce("笔记本");
}
}
}
//定义消费者的任务
class ConsumeTask implements Runnable
{
private Product pro;
ConsumeTask(Product pro)
{
this.pro=pro;
}
public void run()
{
while(true)
{
pro.consume();
}
}
}
class Demo1
{
public static void main(String[] args)
{
Product pro = new Product();
//创建生产任务对象
ProduceTask proTask=new ProduceTask(pro);
//创建消费任务对象
ConsumeTask conTask=new ConsumeTask(pro);
//生产线程
Thread t1=new Thread(proTask);
Thread t2=new Thread(proTask);
//消费线程
Thread t3=new Thread(conTask);
Thread t4=new Thread(conTask);
t1.start();
t2.start();
t3.start();
t4.start();
}
}
二、Lock
JDK1.5单独对锁进行了描述,Lock接口
1:创建Lock的子类对象,Lock lock=new ReentrantLock();
2:把之前放在同步代码块中的代码,放在 lock() 和unlock()两个方法之间
把同步代码块改成Lock之后,出现了 java.lang.IllegalMonitorStateException异常,原因
是wait(),notifyAll()这些方法没有锁了
通过查看api,发现了Condition,Conditon中定义了await(),signal() signalAll()方法
Condition的使用方式:
Condition con=lock.newCondition();//根据锁获取Conditon对象
下面是全部更改完成后的代码
import java.util.concurrent.locks.*;
//需要描述产品
class Product
{
private String name;
private int count;
private boolean flag;
//创建锁对象
private Lock lock=new ReentrantLock();//创建了一把锁
//根据锁得到Condition对象
private Condition pro=lock.newCondition();
private Condition con=lock.newCondition();
//生产功能
public void produce(String name)
{
lock.lock();//获取锁
try{
while(flag)
{
try{pro.await();}catch(InterruptedException e){e.printStackTrace();}
}
this.name=name+"..."+count;
count++;
System.out.println(Thread.currentThread().getName()+"...生产了..."+this.name);
flag=true;
con.signal();
}finally{
lock.unlock();//释放锁
}
}
//消费功能
public void consume()
{
lock.lock();//获取锁
try{
while(!flag)
try{con.await();}catch(InterruptedException e){e.printStackTrace();}
System.out.println(Thread.currentThread().getName()+"...消费了..."+this.name);
flag=false;
pro.signal();
}finally{
lock.unlock();
}
}
}
//定义生产者的任务
class ProduceTask implements Runnable
{
private Product pro;
ProduceTask(Product pro)
{
this.pro=pro;
}
public void run()
{
while(true)
{
pro.produce("笔记本");
}
}
}
//定义消费者的任务
class ConsumeTask implements Runnable
{
private Product pro;
ConsumeTask(Product pro)
{
this.pro=pro;
}
public void run()
{
while(true)
{
pro.consume();
}
}
}
class Demo2
{
public static void main(String[] args)
{
Product pro = new Product();
//创建生产任务对象
ProduceTask proTask=new ProduceTask(pro);
//创建消费任务对象
ConsumeTask conTask=new ConsumeTask(pro);
//生产线程
Thread t1=new Thread(proTask);
Thread t2=new Thread(proTask);
//消费线程
Thread t3=new Thread(conTask);
Thread t4=new Thread(conTask);
t1.start();
t2.start();
t3.start();
t4.start();
}
}
Condition
获取Condition对象
示例代码如下:
只有Condition接口里才有Lock对象
Condition接口里的方法—await()相当于wait()方法
Condition接口里的方法—signal()方法相当于notify()方法
Condition接口里的方法—signalAll()方法相当于notifyAll()方法
三、线程的停止
1.设置标记
2.interrupt方法
class Test implements Runnable
{
boolean flag=true;
public synchronized void run()
{
while(flag)
{
try{
this.wait();
}catch(InterruptedException e)
{
changFlag();
}
System.out.println(Thread.currentThread().getName()+"Hello World!");
}
}
public void changFlag()
{
flag=false;
}
}
class Demo3
{
public static void main(String[] args)
{
Test t=new Test();
Thread t1=new Thread(t);
Thread t2=new Thread(t);
t1.start();
t2.start();
try{
Thread.sleep(10);
}catch(InterruptedException e){
e.printStackTrace();
}
int i=0;
while(true)
{
i++;
if(i==500)
{
//t.changFlag();
t1.interrupt();//中断线程的等待状态
t2.interrupt();//中断线程的等待状态
break;
}
}
}
}
四、守护线程
守护线程:setDaemon(boolean ),可以看成是后台线程,当所有的前台线程全部结束,守护线程的任务代码即使没有执行完也会立刻结束
class Test implements Runnable
{
boolean flag=true;
public synchronized void run()
{
while(flag)
{
try{
this.wait();
}catch(InterruptedException e)
{
changFlag();
}
System.out.println(Thread.currentThread().getName()+"Hello World!");
}
}
public void changFlag()
{
flag=false;
}
}
class Demo4
{
public static void main(String[] args)
{
Test t=new Test();
Thread t1=new Thread(t);
Thread t2=new Thread(t);
t2.setDaemon(true);//设置为守护线程,在线程启动之前设置
t1.start();
t2.start();
try{
Thread.sleep(10);
}catch(InterruptedException e){
e.printStackTrace();
}
int i=0;
while(true)
{
i++;
if(i==500)
{
//t.changFlag();
t1.interrupt();//中断线程的等待状态
//t2.interrupt();//中断线程的等待状态
break;
}
}
}
}
设置守护线程必须要在线程启动之前设置、
五、join
class Test implements Runnable
{
public void run()
{
for(int i=1;i<=10;i++)
{
System.out.println(Thread.currentThread().getName()+"...."+i);
}
}
}
class Demo5
{
public static void main(String[] args)throws InterruptedException
{
Test t=new Test();
Thread t1=new Thread(t);
Thread t2=new Thread(t);
t1.start();
//t1.join();//这时有main,和t1两个线程,main会等待t1全部执行完才执行,main可以使用t1线程的运行结果
t2.start();
t1.join();//这时有main,和t1,t2两个线程,main会等待t1全部执行完才执行,其它线程正常执行
for(int i=1;i<=10;i++)
{
System.out.println(Thread.currentThread().getName()+"...."+i);
}
}
}
六、yield
Thread.yield() :让当前运行的线程从运行状态转到就绪状态,让其他线程获取运行权利,但是
该让出cpu的线程,和其它就绪状态的线程一起挣抢cpu,可能优先抢到cpu
class Test implements Runnable
{
public void run()
{
for(int i=1;i<=10;i++)
{
System.out.println(Thread.currentThread().getName()+"...."+i);
if(i==5)
{
//Thread.yield();
}
}
}
}
class Demo6
{
public static void main(String[] args)throws InterruptedException //主线程创建的子线程,和主线程的优先级是一样的,都是5
{
Test t=new Test();
Thread t1=new Thread(t);
Thread t2=new Thread(t);
Thread t3=new Thread(t);
System.out.println(t1.getPriority()+"===="+t2.getPriority()+"===="+Thread.currentThread().getPriority());
t1.setPriority(Thread.MIN_PRIORITY);
t2.setPriority(Thread.MAX_PRIORITY);//优先级高的只是先执行的概率高,但并不是一定先执行
t1.start();
t2.start();
t3.start();
for(int i=1;i<=10;i++)
{
System.out.println(Thread.currentThread().getName()+"...."+i);
}
}
}
七、优先级
优先级高的只是先执行的概率高,但并不是一定先执行
主线程创建的子线程,和主线程的优先级是一样的,都是5
示例代码在第六条