黑马程序员技术交流社区
标题:
一道多线程问题
[打印本页]
作者:
张丽
时间:
2012-10-6 19:54
标题:
一道多线程问题
package zhangli1;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class LockDemo {
/**
* 生产者消费者实例的1.5以后版本中用Lock来实现。
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
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 static boolean flag=false;
Lock lock=new ReentrantLock();//创建一个锁对象
private Condition producer_con=lock.newCondition();//创建锁上的监视器对象(生产者)
private Condition consumer_con=lock.newCondition();//创建锁上的监视器对象(消费者)
public void set(String name){
lock.lock();
try{
while(flag)
try {
producer_con.await();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
this.name=name+count;
System.out.println(Thread.currentThread().getName()+".生产者.."+this.name);
count++;
consumer_con.signal();
}finally{
lock.unlock();
}
}
public void out(){
lock.lock();
try{
while(!flag)
try {
consumer_con.await();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+"....消费者....."+name);
producer_con.signal();
}finally{
lock.unlock();
}
}
}
class Producer implements Runnable{
Resource r;
Producer(Resource r){
this.r=r;
}
public void run(){
while(true){
r.set("生日蛋糕");
}
}
}
class Consumer implements Runnable{
Resource r;
Consumer(Resource r){
this.r=r;
}
public void run(){
while(true){
r.out();
}
}
}
运行结果如下,问下为什么只出现了两个线程,而没有Thread-2和Thread-3,而且为什么不是生产一个,消费一个的?
Thread-0.生产者..生日蛋糕79377
Thread-0.生产者..生日蛋糕79378
Thread-0.生产者..生日蛋糕79379
Thread-0.生产者..生日蛋糕79380
Thread-0.生产者..生日蛋糕79381
Thread-0.生产者..生日蛋糕79382
Thread-0.生产者..生日蛋糕79383
Thread-0.生产者..生日蛋糕79384
Thread-0.生产者..生日蛋糕79385
Thread-0.生产者..生日蛋糕79386
Thread-0.生产者..生日蛋糕79387
Thread-0.生产者..生日蛋糕79388
Thread-0.生产者..生日蛋糕79389
Thread-0.生产者..生日蛋糕79390
Thread-0.生产者..生日蛋糕79391
Thread-0.生产者..生日蛋糕79392
Thread-1.生产者..生日蛋糕79393
Thread-1.生产者..生日蛋糕79394
Thread-1.生产者..生日蛋糕79395
Thread-1.生产者..生日蛋糕79396
Thread-1.生产者..生日蛋糕79397
Thread-1.生产者..生日蛋糕79398
Thread-1.生产者..生日蛋糕79399
Thread-1.生产者..生日蛋糕79400
Thread-1.生产者..生日蛋糕79401
Thread-1.生产者..生日蛋糕79402
Thread-1.生产者..生日蛋糕79403
Thread-1.生产者..生日蛋糕79404
Thread-1.生产者..生日蛋糕79405
Thread-1.生产者..生日蛋糕79406
Thread-1.生产者..生日蛋糕79407
Thread-1.生产者..生日蛋糕79408
Thread-1.生产者..生日蛋糕79409
Thread-1.生产者..生日蛋糕79410
Thread-1.生产者..生日蛋糕79411
Thread-1.生产者..生日蛋糕79412
Thread-1.生产者..生日蛋糕79413
Thread-1.生产者..生日蛋糕79414
Thread-1.生产者..生日蛋糕79415
Thread-1.生产者..生日蛋糕79416
Thread-1.生产者..生日蛋糕79417
Thread-1.生产者..生日蛋糕79418
Thread-1.生产者..生日蛋糕79419
Thread-1.生产者..生日蛋糕79420
Thread-1.生产者..生日蛋糕79421
Thread-1.生产者..生日蛋糕79422
Thread-1.生产者..生日蛋糕79423
Thread-1.生产者..生日蛋糕79424
Thread-1.生产者..生日蛋糕79425
Thread-1.生产者..生日蛋糕79426
Thread-1.生产者..生日蛋糕79427
Thread-1.生产者..生日蛋糕79428
Thread-1.生产者..生日蛋糕79429
Thread-1.生产者..生日蛋糕79430
Thread-1.生产者..生日蛋糕79431
作者:
李志群
时间:
2012-10-6 21:49
这个程序我没细看 但是建议楼主多运行几次 ,或者 你多开点消耗CPU的程序,这样有可能会看到别的线程了 呵呵
因为这些线程在抢夺CPU资源的时候是不确定的哦 不一定谁先抢到,有可能1线程 和主线程 呛到了 就全执行完了 没等其他线程运行呢,,就会产生楼主你看到的结果呵呵
作者:
古银平
时间:
2012-10-6 22:25
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class LockDemo {
/**
* 生产者消费者实例的1.5以后版本中用Lock来实现。
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
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;
// t1 t2
private Lock lock = new ReentrantLock();
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();//t1,t2
this.name = name+"--"+count++;
System.out.println(Thread.currentThread().getName()+"...生产者.."+this.name);
flag = true;
condition_con.signal();
}
finally
{
lock.unlock();//释放锁的动作一定要执行。
}
}
// t3 t4
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)
{
}
}
}
}
复制代码
这样就能正常运行了
Thread-2...消费者.........生日蛋糕--2873
Thread-0...生产者..生日蛋糕--2874
Thread-3...消费者.........生日蛋糕--2874
Thread-1...生产者..生日蛋糕--2875
Thread-2...消费者.........生日蛋糕--2875
Thread-0...生产者..生日蛋糕--2876
Thread-3...消费者.........生日蛋糕--2876
Thread-1...生产者..生日蛋糕--2877
Thread-2...消费者.........生日蛋糕--2877
Thread-0...生产者..生日蛋糕--2878
Thread-3...消费者.........生日蛋糕--2878
Thread-1...生产者..生日蛋糕--2879
Thread-2...消费者.........生日蛋糕--2879
Thread-0...生产者..生日蛋糕--2880
Thread-3...消费者.........生日蛋糕--2880
Thread-1...生产者..生日蛋糕--2881
Thread-2...消费者.........生日蛋糕--2881
Thread-0...生产者..生日蛋糕--2882
Thread-3...消费者.........生日蛋糕--2882
Thread-1...生产者..生日蛋糕--2883
Thread-2...消费者.........生日蛋糕--2883
Thread-0...生产者..生日蛋糕--2884
Thread-3...消费者.........生日蛋糕--2884
Thread-1...生产者..生日蛋糕--2885
Thread-2...消费者.........生日蛋糕--2885
Thread-0...生产者..生日蛋糕--2886
Thread-3...消费者.........生日蛋糕--2886
Thread-1...生产者..生日蛋糕--2887
Thread-2...消费者.........生日蛋糕--2887
Thread-0...生产者..生日蛋糕--2888
Thread-3...消费者.........生日蛋糕--2888
Thread-1...生产者..生日蛋糕--2889
Thread-2...消费者.........生日蛋糕--2889
Thread-0...生产者..生日蛋糕--2890
Thread-3...消费者.........生日蛋糕--2890
Thread-1...生产者..生日蛋糕--2891
Thread-2...消费者.........生日蛋糕--2891
Thread-0...生产者..生日蛋糕--2892
Thread-3...消费者.........生日蛋糕--2892
Thread-1...生产者..生日蛋糕--2893
Thread-2...消费者.........生日蛋糕--2893
Thread-0...生产者..生日蛋糕--2894
Thread-3...消费者.........生日蛋糕--2894
Thread-1...生产者..生日蛋糕--2895
Thread-2...消费者.........生日蛋糕--2895
Thread-0...生产者..生日蛋糕--2896
Thread-3...消费者.........生日蛋糕--2896
Thread-1...生产者..生日蛋糕--2897
Thread-2...消费者.........生日蛋糕--2897
Thread-0...生产者..生日蛋糕--2898
Thread-3...消费者.........生日蛋糕--2898
Thread-1...生产者..生日蛋糕--2899
Thread-2...消费者.........生日蛋糕--2899
Thread-0...生产者..生日蛋糕--2900
Thread-3...消费者.........生日蛋糕--2900
Thread-1...生产者..生日蛋糕--2901
Thread-2...消费者.........生日蛋糕--2901
Thread-0...生产者..生日蛋糕--2902
Thread-3...消费者.........生日蛋糕--2902
Thread-1...生产者..生日蛋糕--2903
Thread-2...消费者.........生日蛋糕--2903
Thread-0...生产者..生日蛋糕--2904
Thread-3...消费者.........生日蛋糕--2904
Thread-1...生产者..生日蛋糕--2905
Thread-2...消费者.........生日蛋糕--2905
Thread-0...生产者..生日蛋糕--2906
Thread-3...消费者.........生日蛋糕--2906
Thread-1...生产者..生日蛋糕--2907
Thread-2...消费者.........生日蛋糕--2907
Thread-0...生产者..生日蛋糕--2908
Thread-3...消费者.........生日蛋糕--2908
Thread-1...生产者..生日蛋糕--2909
Thread-2...消费者.........生日蛋糕--2909
Thread-0...生产者..生日蛋糕--2910
Thread-3...消费者.........生日蛋糕--2910
Thread-1...生产者..生日蛋糕--2911
Thread-2...消费者.........生日蛋糕--2911
Thread-0...生产者..生日蛋糕--2912
Thread-3...消费者.........生日蛋糕--2912
Thread-1...生产者..生日蛋糕--2913
Thread-2...消费者.........生日蛋糕--2913
Thread-0...生产者..生日蛋糕--2914
Thread-3...消费者.........生日蛋糕--2914
Thread-1...生产者..生日蛋糕--2915
Thread-2...消费者.........生日蛋糕--2915
Thread-0...生产者..生日蛋糕--2916
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2