class SharedData
{
private boolean isNew = false;
private int data;
{
private boolean isNew = false;
private int data;
public synchronized void put(int data)
{
try {
while( isNew ) // new data?
wait(); // unlocked
} catch(InterruptedException e ) {}
this.data = data;
isNew = true;
notifyAll();
}
{
try {
while( isNew ) // new data?
wait(); // unlocked
} catch(InterruptedException e ) {}
this.data = data;
isNew = true;
notifyAll();
}
public synchronized int get()
{
try {
while( !isNew ) // already got?
wait(); // unlocked
} catch(InterruptedException e ) {}
isNew = false;
notifyAll(); // waiting threads cannot wake up
return data; // until this method exits
}
}
{
try {
while( !isNew ) // already got?
wait(); // unlocked
} catch(InterruptedException e ) {}
isNew = false;
notifyAll(); // waiting threads cannot wake up
return data; // until this method exits
}
}
class Writer extends Thread
{
private SharedData shared;
private int data;
public Writer(SharedData shared, int data) {
this.shared = shared;
this.data = data;
}
public void run() {
shared.put(data);
shared.put(data+1);
}
}
this.shared = shared;
this.data = data;
}
public void run() {
shared.put(data);
shared.put(data+1);
}
}
class Reader extends Thread
{
SharedData shared;
{
SharedData shared;
public Reader(SharedData shared) {
this.shared = shared;
}
public void run() {
while(true)
System.out.println( getName() + " got " + shared.get() );
}
}
this.shared = shared;
}
public void run() {
while(true)
System.out.println( getName() + " got " + shared.get() );
}
}
class Wait
{
public static void main(String[] args)
{
SharedData shared = new SharedData();
Reader r1 = new Reader(shared);
Reader r2 = new Reader(shared);
Writer w1 = new Writer(shared, 10);
Writer w2 = new Writer(shared, 20);
r1.start();
r2.start();
w1.start();
w2.start();
}
}
r2.start();
w1.start();
w2.start();
}
}
[오프라인 개강예정강좌, 오프라인교육장에 오시면 보다 자세히 배울 수 있습니다.]
오라클자바커뮤니티에서 운영하는 개발자 전문교육 ,개인80%환급(www.onjprogramming.co.kr)
[주간]
[01/06][기업100%환급]Spring ,MyBatis,Hibernate실무과정
[01/06][기업100%환급]SQL기초에서 Schema Object까지
[01/06]C#,ASP.NET마스터
[01/13]iPhone 하이브리드 앱 개발 실무과정
[01/13][기업100%환급]PL/SQL,ORACLE HINT,TUNING
[01/13][기업100%환급]자바기초에서 JDBC, Servlet/JSP까지
[01/13][기업100%환급]HTML5,JavaScript,Ajax,jQUERY,JSON
[01/16][채용확정]오라클자바실무개발자신입과정
[평일야간]
[01/07]자바기초에서JSP,Ajax,jQuery,Spring3.2,MyBatis까지
[01/07]안드로이드개발자과정
[01/08]C#,ASP.NET마스터
[01/09]iPhone하이브리드앱개발실무과정
[01/09]웹퍼블리싱 마스터
[01/09]Spring3.X, MyBatis, Hibernate실무과정
[01/24]SQL초보에서실전전문가까지
[주말]
[01/04]닷넷실무자를위한WPF개발자과정
[01/04]Spring3.X,MyBatis,Hibernate실무과정
[01/11]C#,ASP.NET마스터
[01/11]JAVA&WEB프레임워크실무과정
[01/11]안드로이드개발자과정
[01/11]SQL초보에서전문가까지
[01/18]웹퍼블리싱 마스터
댓글 없음:
댓글 쓰기