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 - 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…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.
Contents
-
-
-
-
-
(Locked)
Pausing execution of a thread3m 52s
-
(Locked)
Interrupting a thread5m 42s
-
(Locked)
Implementing thread interruptions6m 13s
-
(Locked)
Making a thread to wait until another thread terminates5m 39s
-
(Locked)
Other useful methods of the Thread class2m 42s
-
(Locked)
Challenge: Webinar polling system1m 53s
-
(Locked)
Solution: Webinar polling system5m 9s
-
(Locked)
-