publicstaticvoid main(String[] args) throws Exception { /* The value of the threshold determines for how many seconds *themainthreadmustwaitfortheworkerthread.Onhighly *loadedsystemsitcantakeawhileuntiltheworkerthread *obtainsCPUtimeandisabletocheckifitwasinterrupted *bythemainthread.Thehigherthethresholdthelikelier *theworkerthreadcancheckifitwasinterrupted(thatis *requiredforsuccessultestexecution).
*/ int threshold = 100;
if (args.length != 1) {
System.out.println("Incorrect number of arguments");
System.exit(1);
}
if (threshold < 1) {
System.out.println("Threshold must be at least 1");
System.exit(1);
}
Thread workerThread = newThread("worker") { publicvoid run() {
System.out.println("Worker thread: running..."); while (!Thread.currentThread().isInterrupted()) {
}
System.out.println("Worker thread: bye");
}
};
System.out.println("Main thread: starts a worker thread...");
workerThread.start();
System.out.println("Main thread: waits 5 seconds after starting the worker thread");
workerThread.join(5000); // Wait 5 sec to let run() method to be compiled
int ntries = 0; while (workerThread.isAlive() && ntries < threshold) {
System.out.println("Main thread: interrupts the worker thread...");
workerThread.interrupt(); if (workerThread.isInterrupted()) {
System.out.println("Main thread: worker thread is interrupted");
}
ntries++;
System.out.println("Main thread: waits 1 second for the worker thread to die...");
workerThread.join(1000); // Wait 1 sec and try again
}
if (ntries == threshold) {
System.out.println("Main thread: the worker thread did not die after " +
ntries + " seconds have elapsed");
System.exit(97);
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.