1つのスレッドが特定の時間待機状態にある場合に、別のスレッドを実行したいという方法で、2つのスレッドを監視する方法を知りたいと思いました...
私がしていることに特にこだわる。私は2つのスレッドを持っています..ライタースレッド:
writer = new Thread() {
public void run() {
try {
Util.copyStream(remoteInput, localOutput);
} catch(IOException e) {
e.printStackTrace();
System.exit(1);
}
}
};
writer.setPriority(Thread.currentThread().getPriority() + 1);
writer.start();
reader.setDaemon(true);
reader.start();
try {
writer.join();
reader.interrupt();
} catch(InterruptedException e) {
}
とリーダースレッド:
reader = new Thread() {
public void run() {
int ch;
try {
while(!interrupted() && (ch = localInput.read()) != -1) {
System.out.print((char)ch);
localOutput.write(ch);
if (ch==10) {
remoteOutput.write(ch);
remoteOutput.flush();
sleep(1000);
continue;
}
remoteOutput.write(ch);
remoteOutput.flush();
}
} catch(Exception e) {
e.printStackTrace();
}
}
};
したがって、ライタースレッドが「remoteInput」から「localOutput」に特定の時間(たとえば3秒以上)書き込みを行っていない場合は、リーダースレッドを「実行」して、リーダーが「localInput」から読み取るようにする必要があります。 「remoteOutput」への書き込み。localInputとremoteInputはInputStreamsですが、remoteInputとremoteOutputは単純なOutputStreamsです。また、 java.util.Timerを使用してこれを実行できるかどうかも知りたいと思い ました。助けてください。よろしくお願いします。