Q: I have some doubts about how to solve in the correct way the assignment you gave us for the Distributed System course.
More in details I doubt about when the program should terminate.
If method stop() is deprecated, how could we "end" the execution of our program?


A: You are right that stop() should not be used. Instead a pattern one
can use to make a thread stop after X milliseconds is the following:

class MyThread extends Threads {
...public void run() {
.....long start = System.currentTimeMillis();
.....while (System.currentTimeMillis()<(start+X)) {
.......// do something
.....}
...}
}

The main method then could start the thread and also wait for X plus a
little bit more before outputting the final results:

public static void main(String[] args) {
..MyThread t = new MyThread();
..t.start();
..Thread.sleep(X+100);
..// The main thread gets here only after the thread t has finished. Now we can output final results or similar.
..System.out.println("Output...");
}

Zuletzt geändert: Wednesday, 25. February 2015, 17:27