U E D R , A S I H C RSS

Java/스레드재사용

아래 코드에 문제가 있는것 같아요. 분명 두개의 메소드가 같아보이는데 (주석처리된 run() 메소드와 run() 메소드) 한개만 되 나머지 한개는 에러가 납니다(unreachable statement) - 임인택

waitfortarget() 콜하기 전에 중괄호가 두개 있 하나 있는 차이인것 같은디?? 음.. 지금 주석처리 안한 run에서 에러가 나는거야?
만약 그러면.. 무한루프때메.. wait..어쩌구 함수에 도달 못해서.. '도달할수없는 문장' 이라는 에러가 나는것이 아닐까..??? (._.);
주석처리한것에서 나면.. 음.. 잘 모르겄넹.. ㅋㅋ;
-해성

소스코드

~cpp 

import java.util.*;

public class ReThread implements Runnable {
	
	protected Runnable target;
	private static Vector threads = new Vector();
	private ReThread reThread;
	private Thread thread;
	private static int id=0;
	private static synchronized int getID() { return id++;}
	
	public ReThread(Runnable target) {
		this.target = target;
	}
	
	public synchronized void start() {
		if((thread==null) && (reThread==null)) {
			synchronized(threads) {
				if(threads.isEmpty()) {
					thread = new Thread(this, "ReThread-" + getID());
					thread.start();
				}
				else {
					reThread = (ReThread)threads.lastElement();
					threads.setSize(threads.size()-1);
					reThread.start0(this);
				}
			}
		}		
	}
	
	protected synchronized void start0(ReThread reThread) {
		this.reThread = reThread;
		target = reThread.target;
		notify();
	}
	
	public synchronized void interrupt () {
		if ((target != null) && ((thread != null) ^ (reThread != null))) {
			if (thread != null) {
				thread.interrupt ();
			} else {
				reThread.interrupt0 (this);
			}
		}
	}
	
	protected synchronized void interrupt0(ReThread reThread) {
		if((target!=null) && ((reThread==this.reThread))) {
			thread.interrupt();
		}
	}
	
	public void run() {
		while(true) {
			try {
				target.run();
			} catch(RuntimeException ex) {
				ex.printStackTrace();
			}
		}
		waitForTarget();
	}
	/*public void run () {
    while (true) {
      try {
        target.run ();
      } catch (RuntimeException ex) {
        ex.printStackTrace ();
      }
      waitForTarget ();
    }
  }*/
	
	protected synchronized void waitForTarget() {
		target=null;
		threads.addElement(this);
		while(target==null) {
			try {
				wait();
			} catch(InterruptedException ignored) {			}
		}
	}
}
Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2021-02-07 05:23:33
Processing time 0.0076 sec