Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Need help with this java problem. Thanks. Please simple as possible so I can understand what im doing wrong. package longtask; public class Main {

Need help with this java problem. Thanks. Please simple as possible so I can understand what im doing wrong.

image text in transcribed

package longtask;

public class Main {

public static void main(String[] args) { System.out.println("Main thread started.");

// Perform a long task System.out.println("Long task started."); try { Thread.sleep(10000); } catch (InterruptedException ex) {} System.out.println("Long task finished.");

// Finish the main thread System.out.println("Main thread still running."); for (int i = 1; i

Exercise 18-3 Perform a long running task In this exercise, you'l1 allow the main thread to run while a long running task runs on another thread in the background. When you've completed this exercise, your console should look similar to the following console. However, it might look slightly different depending on whether the thread manager selects the main thread or the long task thread to run first. Main thread started Main thread still running Main thread running: 1 Long task thread started Main thread running: 2 Main thread running: 3 Main thread running: 4 Main thread running: 5 Main thread finished Long task thread finished Review the application 15. Open the project named ch18_ex3_LongTask in the extraexstarts folder 16. Review the code for the Main class. Note that the main thread begins by performing a long task that takes at least 10 seconds. Then, it continues by counting from 1 to 5 with at least 1 second between each number 17. Run the application to see how it works. Note that the long task prevents the counter in the main method from executing until the long task has finished. Add a thread to the application 18. Add a class named TaskThread that implements the Runnable interface 19. Move the code that performs the long task from the main method of the Main class into the run method of the TaskThread class 20. In the Main class, use the Thread class to start the thread defined by the TaskThread class 21. Run the application again to make sure it works correctly. Note that the counter in the main method starts immediately. That's because the run method of the TaskThread class is now running on its own thread

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

Is the person willing to deal with the consequences?

Answered: 1 week ago

Question

Was there an effort to involve the appropriate people?

Answered: 1 week ago