`
shonghuanc
  • 浏览: 81639 次
文章分类
社区版块
存档分类
最新评论

线程小例子

阅读更多
//定义一个生产类
class Producer extends Thread
{  
     
    Producer()
    {
        setPriority(Thread.MAX_PRIORITY);
    }
     public void run()
     {
         Store store = new Store();
         for(int i=0;i <15;i++)
         {
            store.store_in();
         }
     }
}
//定义一个消费类
class Customer implements Runnable
{   
    long time = 100;
    Customer()
    {
    }
     public void run()
     {   
           try{
                Thread.sleep(time);
            }catch (InterruptedException e){throw new RuntimeException(e);} 
         Store store = new Store();
          for(int i=0;i <15;i++)
         {
            store.store_out();          
         }
     }
}

public class Store
{
     private static int productors = 0;
     private final int MAX_STORE = 10;
     private final int MIN_STORE = 1;
//定义一个同步入货方法
     public synchronized void store_in()
        {  
            if (productors >= MAX_STORE)
             {
                 try {
       System.out.println("库存数量太多!!!!!");
       wait();
                } catch (Exception e){}        
             }
            else
                {
      productors ++;
                    System.out.println("执行入库,库存数量为"+ productors);
                    notifyAll();
                }
        } 
//定义一个同步取货方法
     public synchronized void store_out()
     {
           if (productors  < MIN_STORE)
             { 
                 try {
                    System.out.println("库存数量不足!!!!!");
                     wait();
               }catch (Exception e){}   
             }
            else 
            {
                productors --;
                System.out.println("执行出库,库存数量为"+productors); 
                notifyAll(); 
             }
     }
//定义查库存方法
      public int getStore()
      {
          return productors;
      }
//主涵数,启动两个线程
     public static void main(String args [])
     {
         Producer p = new Producer();
         p.start();
         Store store = new Store();
         
        Thread c = new Thread(new Customer());
         c.start();
        System.out.println(store.getStore());
     } 
}

请问Thread C 如何唤醒Producer P,尽量给出修改结果,join()方法起什么作用,如何调,十分感谢


问题点数:120 回复次数:6 显示所有回复显示星级回复显示楼主回复 修改 删除 举报 引用 回复  


加为好友
发送私信
在线聊天
java2000_net
╔──────────╗  │老紫竹Java2000.net│  ╚──────────╝
等级:
发表于:2008-01-26 19:35:181楼 得分:0
notify();
notifyAll();

public final void notifyAll()
唤醒在此对象监视器上等待的所有线程。线程通过调用其中一个 wait 方法,在对象的监视器上等待。 
直到当前线程放弃此对象上的锁定,才能继续执行被唤醒的线程。被唤醒的线程将以常规方式与在该对象上主动同步的其他所有线程进行竞争;例如,唤醒的线程在作为锁定此对象的下一个线程方面没有可靠的特权或劣势。 

此方法只应由作为此对象监视器的所有者的线程来调用。有关线程能够成为监视器所有者的方法的描述,请参阅 notify 方法。


抛出:
IllegalMonitorStateException - 如果当前线程不是此对象监视器的所有者。
另请参见:
notify(), wait()

修改 删除 举报 引用 回复  

加为好友
发送私信
在线聊天
dongchaotiantian

等级:
发表于:2008-01-27 11:52:342楼 得分:0
能给出代码的修改结果吗

修改 删除 举报 引用 回复  

加为好友
发送私信
在线聊天
java2000_net
╔──────────╗  │老紫竹Java2000.net│  ╚──────────╝
等级:
发表于:2008-01-27 12:08:423楼 得分:0
你想在哪里唤醒?就在哪里写上这个代码!


Java codeStore store = new Store();
notifyAll();// 这样就行
for (int i = 0; i < 15; i++) {
    store.store_out();
}



修改 删除 举报 引用 回复  

加为好友
发送私信
在线聊天
java2000_net
╔──────────╗  │老紫竹Java2000.net│  ╚──────────╝
等级:
发表于:2008-01-27 12:09:154楼 得分:0
Java codeStore store = new Store();
for (int i = 0; i < 15; i++) {
    store.store_out();
    notifyAll();// 这样也许更好
}



修改 删除 举报 引用 回复  

加为好友
发送私信
在线聊天
dracularking
苦行魔君
等级:
发表于:2008-01-27 12:54:245楼 得分:0
join(): Waits for this thread to die.
假如在b线程区块中调用a.join(),就会阻塞当前b线程直至a线程运行完毕


Java codepublic class Test {
    public static void main(String[] args) {
        bao b = new bao();

        b.start();

        try {
            Thread.sleep(5000);
            b.join(100);
        }
        catch (Exception e) {
        }

        int i = 0;
        while (true) {
            System.out.println("mainThread: "
                    + Thread.currentThread().getName());
        }

    }
}

class bao extends Thread {
    public void run() {
        while (true) {
            System.out.println("run()Thread: "
                    + Thread.currentThread().getName());
        }
    }
}



以上例子副线程会一直运行直至5.1秒后停止阻塞 主副线程并行

修改 删除 举报 引用 回复  

加为好友
发送私信
在线聊天
caiming250
我坚强的爬了起来,拖着自己的尸体继续前进
等级:
发表于:2008-01-27 13:10:166楼 得分:0
危险...:
Java code

    // 定义一个同步入货方法
    public synchronized void store_in() {
        if (productors >= MAX_STORE) {
            try {
                System.out.println("库存数量太多!!!!!");
                wait();
            } catch (Exception e) {
            }
        } else {
            productors++;
            System.out.println("执行入库,库存数量为" + productors);
            notifyAll();
        }
    }
    // 定义一个同步取货方法
    public synchronized void store_out() {
        if (productors < MIN_STORE) {
            try {
                System.out.println("库存数量不足!!!!!");
                wait();
            } catch (Exception e) {
            }
        } else {
            productors--;
            System.out.println("执行出库,库存数量为" + productors);
            notifyAll();
        }
    }





这段代码危险!  wait(); 方法最好放在一个条件判断循环(while)里面   要不然 在notifyAll唤醒两个以上线程 取 访问productors 的时候会出现你不希望的结果

修改建议:

Java code
// 定义一个同步入货方法
    public synchronized void store_in() {
        try {
            while (productors >= MAX_STORE) {
                System.out.println("库存数量太多!!!!!");
                wait();
            }
            productors++;
            System.out.println("执行入库,库存数量为" + productors);
            notifyAll();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    // 定义一个同步取货方法
    public synchronized void store_out() {
        try {
            while (productors < MIN_STORE) {
                System.out.println("库存数量不足!!!!!");
                wait();
            }
            productors--;
            System.out.println("执行出库,库存数量为" + productors);
            notifyAll();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }




分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics