Chương 2: Lập trình đa luồng
Trang 1 trong tổng số 1 trang
Chương 2: Lập trình đa luồng
Bài toán: Producer - Costumer
*Không đồng bộ:
*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
- 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
Similar topics
» Cách xóa file tmp của KAS tăng dung lượng ổ C
» Lập trình SHELL
» Hỗ trợ lập trình Java
» Tổng hợp đề thi và bài giải môn LẬP TRÌNH MẠNG các kì
» Tổng hợp đề thi và bài giải môn LẬP TRÌNH WEB các kì (đề TỰ LUẬN)
» Lập trình SHELL
» Hỗ trợ lập trình Java
» Tổng hợp đề thi và bài giải môn LẬP TRÌNH MẠNG các kì
» Tổng hợp đề thi và bài giải môn LẬP TRÌNH WEB các kì (đề TỰ LUẬN)
Trang 1 trong tổng số 1 trang
Permissions in this forum:
Bạn không có quyền trả lời bài viết
|
|