Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Exercise 18-3 Perform a long running task In this exercise, youll allow the main thread to run while a long running task runs on another

Exercise 18-3 Perform a long running task

In this exercise, youll allow the main thread to run while a long running task runs on another thread in the background. When youve 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

1.Open the project named ch18_ex3_LongTask in the extra_ex_starts folder.

2.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.

3.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

4. Add a class named TaskThread that implements the Runnable interface.

5. Move the code that performs the long task from the main method of the Main class into the run method of the TaskThread class.

6. In the Main class, use the Thread class to start the thread defined by the TaskThread class.

7. Run the application again to make sure it works correctly. Note that the counter in the main method starts immediately. Thats because the run method of the TaskThread class is now running on its own thread.

Main.java

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 < 6; i++) { System.out.println("Main thread running: " + i); try { Thread.sleep(1000); } catch (InterruptedException ex) {} } System.out.println("Main thread finished."); } }

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

Genomes And Databases On The Internet A Practical Guide To Functions And Applications

Authors: Paul Rangel

1st Edition

189848631X, 978-1898486312

More Books

Students also viewed these Databases questions