Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Instructions A thread can be in one of the following states: State Description NEW A thread that has not yet started is in this state.

Instructions

A thread can be in one of the following states:

State Description
NEW A thread that has not yet started is in this state.
RUNNABLE A thread executing in the Java virtual machine is in this state.
BLOCKED A thread that is blocked waiting for a monitor lock is in this state.
WAITING A thread that is waiting indefinitely for another thread to perform a particular action is in this state.
TIMED_WAITING A thread that is waiting for another thread to perform an action for up to a specified waiting time is in this state.
TERMINATED A thread that has exited is in this state.

Create two (2) threads named by the user. Display their various states using getState( ).

In the main method:

  1. Enable user input.
  2. Create two (2) threads.
  3. Ask the user to enter a name for each thread.
  4. Show the names and states of the threads.
  5. Start the threads.
  6. Have the threads sleep for half a second.
  7. Show the names and states of the threads.

In the run( ) method, show the name and state of the current thread.

Sample Output:

Name your first thread: Task A Name your second thread: Task B Task A is NEW Task B is NEW

Starting the threads... Task A is RUNNABLE Task B is RUNNABLE

After sleep... Task A is TERMINATED Task B is TERMINATED

Step by Step Solution

There are 3 Steps involved in it

Step: 1

import javautilScanner import javautilconcurrentTimeUnit public class ThreadStates public static void mainString args Scanner scanner new ScannerSyste... 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

Microeconomics An Intuitive Approach with Calculus

Authors: Thomas Nechyba

1st edition

538453257, 978-0538453257

More Books

Students also viewed these Programming questions