[CODE type="java"]
/** 쓰레드 연습 **/
class Data{
[tab]
[tab]private String data;
[tab]private boolean is_saved = false;
[tab]
[tab]public synchronized void setData(String arg){
[tab][tab]
[tab][tab] try{
[tab][tab][tab]
[tab][tab][tab]
[tab][tab][tab] if(is_saved){
[tab][tab][tab][tab] wait();
[tab][tab][tab] }
[tab][tab] }
[tab][tab] catch (Exception e) {
[tab][tab][tab] System.out.println("SET error");
[tab][tab] }
[tab][tab]
[tab][tab] data = arg;
[tab][tab] is_saved = true;
[tab][tab] notify();
[tab][tab]
[tab]}
[tab]public synchronized String getData(){
[tab][tab]
[tab][tab] try{
[tab][tab][tab] if(!is_saved)
[tab][tab][tab][tab] wait();
[tab][tab][tab]
[tab][tab] }
[tab][tab] catch (Exception e) {
[tab][tab][tab] System.out.println("GET error");
[tab][tab] }
[tab][tab]
[tab][tab] String r = data;
[tab][tab] data = "";
[tab][tab] is_saved =false;
[tab][tab] notify();
[tab][tab] return r;
[tab]}
[tab]
[tab]
[tab]public boolean is_saved(){
[tab][tab] return is_saved;[tab][tab]
[tab]}
[tab]
}
class Producer extends Thread{
[tab]Data s;
[tab]Producer(String msg, Data ss){
[tab][tab] s=ss;
[tab][tab] System.out.println(msg);[tab][tab]
[tab]}
[tab]public void run(){
[tab][tab] int i=0;
[tab][tab] while(i<10){
[tab][tab][tab] try{
[tab][tab][tab] sleep((long)(Math.random()*1000));
[tab][tab][tab] }
[tab][tab][tab] catch (Exception e) {
[tab][tab][tab][tab] // TODO: handle exception
[tab][tab][tab] }
[tab][tab][tab]
[tab][tab][tab] System.out.println( "producer is running : " +i);
[tab][tab][tab] s.setData("DATA ("+i+"): " + produce());
[tab][tab][tab][tab] i++;
[tab][tab]
[tab][tab] }//end while
[tab]}//end run
[tab]
[tab]private String produce(){
[tab][tab] int r ;
[tab][tab] r =(int)( Math.random()*10);
[tab][tab] return "" + r;
[tab]}
[tab]
}
class Consumer extends Thread{
[tab]Data s;[tab]
[tab]
[tab]Consumer(String msg,Data ss){
[tab][tab] s = ss;
[tab]
[tab][tab] System.out.println(msg);
[tab]}
[tab]
[tab]public void run(){
[tab][tab] int i=0;
[tab][tab] String data="";
[tab][tab] while(i<10){
[tab][tab][tab] try{
[tab][tab][tab][tab] sleep((long)(Math.random()*1000));
[tab][tab][tab][tab] }
[tab][tab][tab][tab] catch (Exception e) {
[tab][tab][tab][tab][tab] // TODO: handle exception
[tab][tab][tab][tab] }
[tab][tab][tab][tab]
[tab][tab][tab] System.out.println( "consumer is running : " + i);
[tab][tab][tab] data =s.getData();
[tab][tab][tab] System.out.println(">> " + data);
[tab][tab][tab] i++;
[tab][tab][tab]
[tab][tab] }//end while
[tab]}//end run[tab]
}
class Ttest{
[tab]public static void main(String args[])
[tab]{
[tab][tab] Data data = new Data();
[tab][tab] Producer t1 = new Producer("P thread is ready", data);
[tab][tab] Consumer c1 = new Consumer("C thread is ready", data);
[tab][tab]
[tab][tab]
[tab][tab] t1.start();
[tab][tab] c1.start();
[tab][tab]
[tab][tab]
[tab][tab]
[tab][tab][tab][tab]
[tab]}[tab][tab]
}
/**
class Data{
[tab]
[tab]private String data;
[tab]private boolean is_saved = false;
[tab]
[tab]public void setData(String arg){
[tab][tab] data = arg;
[tab][tab] is_saved = true;
[tab]}
[tab]public String getData(){
[tab][tab] String r = data;
[tab][tab] data = "";
[tab][tab] is_saved =false;
[tab][tab] return r;
[tab]}
[tab]
[tab]
[tab]public boolean is_saved(){
[tab][tab] return is_saved;[tab][tab]
[tab]}
[tab]
}
class Producer extends Thread{
[tab]Data s;
[tab]Producer(String msg, Data ss){
[tab][tab] s=ss;
[tab][tab] System.out.println(msg);[tab][tab]
[tab]}
[tab]public void run(){
[tab][tab] int i=0;
[tab][tab] try{
[tab][tab][tab] while(i<10){
[tab][tab][tab][tab]
[tab][tab][tab][tab] System.out.println( "producer is running : " +i);
[tab][tab][tab][tab] if(s.is_saved()){
[tab][tab][tab][tab][tab] yield();
[tab][tab][tab][tab] }
[tab][tab][tab][tab] else{
[tab][tab][tab][tab][tab] s.setData("DATA ("+i+"): " + produce());
[tab][tab][tab][tab][tab] i++;
[tab][tab][tab][tab] }
[tab][tab][tab][tab]
[tab][tab][tab] }//end while
[tab][tab] }
[tab][tab] catch (Exception e) {
[tab][tab][tab] System.out.println(e);
[tab][tab][tab] // TODO: handle exception
[tab][tab] }
[tab]}//end run
[tab]
[tab]private String produce(){
[tab][tab] int r ;
[tab][tab] r =(int)( Math.random()*10);
[tab][tab] return "" + r;
[tab]}
[tab]
}
class Consumer extends Thread{
[tab]Data s;[tab]
[tab]
[tab]Consumer(String msg,Data ss){
[tab][tab] s = ss;
[tab]
[tab][tab] System.out.println(msg);
[tab]}
[tab]
[tab]public void run(){
[tab][tab] int i=0;
[tab][tab] String data="";
[tab][tab] try{
[tab][tab][tab] while(i<10){
[tab][tab][tab][tab] System.out.println( "consumer is running : " + i);
[tab][tab][tab][tab] if(s.is_saved()){
[tab][tab][tab][tab][tab][tab] data =s.getData();
[tab][tab][tab][tab][tab][tab] System.out.println(">> " + data);
[tab][tab][tab][tab][tab][tab] //notifyAll();
[tab][tab][tab][tab][tab][tab] i++;
[tab][tab][tab][tab] }
[tab][tab][tab][tab] else{
[tab][tab][tab][tab][tab] yield();
[tab][tab][tab][tab][tab] yield();
[tab][tab][tab][tab][tab]
[tab][tab][tab][tab][tab] //this.wait();
[tab][tab][tab][tab][tab] //this.suspend();
[tab][tab][tab][tab] }
[tab][tab][tab][tab]
[tab][tab][tab] }//end while
[tab][tab] }
[tab][tab] catch (Exception e) {
[tab][tab][tab] System.out.println(e);
[tab][tab][tab] // TODO: handle exception
[tab][tab] }
[tab]}//end run[tab]
}
class Ttest{
[tab]public static void main(String args[])
[tab]{
[tab][tab] Data data = new Data();
[tab][tab] Producer t1 = new Producer("P thread is ready", data);
[tab][tab] Consumer c1 = new Consumer("C thread is ready", data);
[tab][tab]
[tab][tab]
[tab][tab] t1.start();
[tab][tab] c1.start();
[tab][tab]
[tab][tab]
[tab][tab]
[tab][tab][tab][tab]
[tab]}[tab][tab]
}
**/
[/HTML][/CODE]
Trackback Address :: 이 글에는 트랙백을 보낼 수 없습니다