Answered step by step
Verified Expert Solution
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:
- Enable user input.
- Create two (2) threads.
- Ask the user to enter a name for each thread.
- Show the names and states of the threads.
- Start the threads.
- Have the threads sleep for half a second.
- 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...Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started