From the course: Java Threads

Unlock this course with a free trial

Join today to access over 25,300 courses taught by industry experts.

Solution: Webinar polling system

Solution: Webinar polling system - Java Tutorial

From the course: Java Threads

Solution: Webinar polling system

- [Instructor] I'll walk you through the solution that I came up with. In the voting runnable, first, I've generated a random number between zero and thousand by simply using math.random and 2000. I've assigned it to a double variable to post the thread running the task for this random amount of time, I call the thread.sleep method passing in the random number of milliseconds. I've obtained the long value of it. You learned in this chapter that the sleep method could throw an interrupted exception. So I've handled it by surrounding the method call with try-catch. Inside the run method after line number 20, I should pause the thread running these tasks for two seconds. So I've called thread.sleep passing in 2000 milliseconds. I surrounded the method call with a try-catch block. In the voting runnable class here, to handle the stopping of the task, I've defined this Boolean flag do stop which I've set false by…

Contents