Chia sẻ kiến thức
Bạn có muốn phản ứng với tin nhắn này? Vui lòng đăng ký diễn đàn trong một vài cú nhấp chuột hoặc đăng nhập để tiếp tục.

Chương 2: Lập trình đa luồng

Go down

Chương 2: Lập trình đa luồng Empty Chương 2: Lập trình đa luồng

Bài gửi  Admin Wed Sep 04, 2013 2:52 am

Bài toán: Producer - Costumer
*Không đồng bộ:
Code:

public class Synch_ex{
   public class Product
   {
      private int view = 0;
      public boolean isRemainingview;
      public int get(){
         System.out.println("\t Consumer get: " + this.view);
         return view;
      }//get
      public void set(int piview){
         this.view = piview;
         System.out.println("Producer set " + this.view);
      }
   }//end class Product

   public class Producer extends Thread
   {
      private Product sp;
      Producer(Product pProduct){
         sp = pProduct;
      }
      public void run(){
         for(int i=0; i<10; i++){
            sp.set(i);
            try{
               Thread.sleep(500);
            }//try
            catch(InterruptedException ie){}
         }//for
      }//run
   }//end class Producer

   public class Consumer extends Thread
   {
      private Product sp;
      Consumer(Product pProduct){
         sp = pProduct;
      }
      public void run(){
         for(int i=0; i<10; i++){
            sp.get();
            try{
               Thread.sleep(500);
            }//try
            catch(InterruptedException ie){}
         }//for
      }//run
   }//end class Consumer

   public static void main(String[] args){
      Synch_ex t = new Synch_ex();
      Product s = t.new Product();
      //2 thread cung thuc hien song song
      Producer p = t.new Producer(s);      
      Consumer c = t.new Consumer(s);
      p.start();
      c.start();
   }
}//end class Synch_ex
*Đồng bộ:
Code:

public class Synch_ex{
   public class Product
   {
      private int view = 0;
      public boolean isRemainingview;
      public synchronized int get(){
         while(isRemainingview == false){
            try{
               wait();
            }//try
            catch(InterruptedException ie){}
         }//while
         isRemainingview = false;
         System.out.println("\t Consumer get: " + this.view);
         //Notify Producer to produce new view
         notifyAll();
         return view;
      }//get
      public synchronized void set(int piview){
         while(isRemainingview == true){
            try{
               wait();
            }
            catch(InterruptedException ie){}
         }//while
         this.view = piview;
         isRemainingview = true;
         System.out.println("Producer set " + this.view);
         //Notify Consumer get this view
         notifyAll();
      }
   }//end class Product

   public class Producer extends Thread
   {
      private Product sp;
      Producer(Product pProduct){
         sp = pProduct;
      }
      public void run(){
         for(int i=0; i<10; i++){
            sp.set(i);
            try{
               Thread.sleep(500);
            }//try
            catch(InterruptedException ie){}
         }//for
      }//run
   }//end class Producer

   public class Consumer extends Thread
   {
      private Product sp;
      Consumer(Product pProduct){
         sp = pProduct;
      }
      public void run(){
         for(int i=0; i<10; i++){
            sp.get();
            try{
               Thread.sleep(1900);
            }//try
            catch(InterruptedException ie){}
         }//for
      }//run
   }//end class Consumer

   public static void main(String[] args){
      Synch_ex t = new Synch_ex();
      Product s = t.new Product();
      //2 thread cung thuc hien song song
      Producer p = t.new Producer(s);      
      Consumer c = t.new Consumer(s);
      p.start();
      c.start();
   }
}//end class Synch_ex
Admin
Admin
Admin

Tổng số bài gửi : 218
Reputation : 22
Join date : 17/11/2012
Age : 32

https://elcit.forumvi.com

Về Đầu Trang Go down

Về Đầu Trang

- Similar topics

 
Permissions in this forum:
Bạn không có quyền trả lời bài viết