Question: This problem tests your knowledge of Threads. This will be a simple program (called Threads1.java) to create several threads and a simple counter. This is

This problem tests your knowledge of Threads. This will be a simple program (called Threads1.java) to create several threads and a simple counter. This is not a GUI program. The main program will instantiate an object of class Threads1, calling its constructor. The constructor will generate the following output:

As shown, it will create 5 threads named 1 through 5 as shown above. The threads will share a common (global) counter which is set to 50 before any of them is started. The threads will loop. On each iteration they will, in a thread-safe manner: Subtract 1 from the shared counter Print the thread name and the counter NOTE: since the subtraction happens before printing, the first value printed is 49 and the last value is 0. Sleep for 2 msec. (unsynchronized) When the counter reaches 0, the thread will terminate (return from the run() method). Note that the count is decreasing and does not duplicate. Also note that the thread numbers are not in any particular order. Your thread numbers will NOT be in the same order as shown, nor should they be the same on any two runs. The counter should count down, in order, from 49 to 0. The example above does this, although the beginning lines have scrolled off the top. Once all threads have finished, the code that created the threads should print out the common counter. Note: There are only 5 threads (besides the main thread).

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!