Multi Thread in
Multi Thread in
simple multithreading Java program that demonstrates how threads work in Java:
t1.start();
t2.start();
this.name = name;
try {
} catch (InterruptedException e) {
e.printStackTrace();
We then start the threads by calling their start() methods. This causes
the run() method of each thread to be executed in a separate thread of
execution.
In the run() method of the MyThread class, we loop from 1 to 5 and print
out the thread name and the current value of i. We then sleep for a
random amount of time using the Thread.sleep() method to simulate
some work being done by the thread.
When you run the program, you should see output like this:
Thread 1: 1
Thread 2: 1
Thread 1: 2
Thread 2: 2
Thread 1: 3
Thread 2: 3
Thread 1: 4
Thread 2: 4
Thread 2: 5
Thread 1: 5
As you can see, the two threads are executing concurrently, interleaving their
output in an unpredictable way. This demonstrates how multithreading can be
used to perform multiple tasks simultaneously.
Java program that creates a thread which will display prime numbers after every
5 seconds:
public class PrimeNumberThread extends Thread {
public void run() {
int num = 2;
while (true) {
if (isPrime(num)) {
System.out.println(num);
}
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
num++;
}
}
Java program that creates two threads, Thread1 and Thread2, and sets the
maximum priority to Thread2 and the minimum priority to Thread1:
thread1.setPriority(Thread.MIN_PRIORITY);
thread2.setPriority(Thread.MAX_PRIORITY);
thread1.start();
thread2.start();
}
}
What is thread?
threads are a powerful tool in Java programming that allow for parallel execution
of code, enabling better performance and increased responsiveness in complex
applications.
A Java program can create multiple threads by extending the Thread class
or by implementing the Runnable interface. When a thread is created, it is
given a priority, which determines the order in which it is scheduled to run
by the Java Virtual Machine (JVM).
In Java, threads are scheduled for execution by the JVM based on their
priority. Higher priority threads are given preference over lower priority
threads. By default, all threads are assigned the priority of the thread that
created them.
thread1.setPriority(Thread.MAX_PRIORITY);
thread2.setPriority(Thread.MIN_PRIORITY);
thread1.start();
thread2.start();
In the above example, we create two threads using the Thread class and set
their priorities using the setPriority() method. The Thread.MAX_PRIORITY
constant is used to set the priority of thread1 to the maximum value, while the
Thread.MIN_PRIORITY constant is used to set the priority of thread2 to the
minimum value. Finally, we start both threads using the start() method.
Thread Life Cycle:
In Java, a thread goes through several states in its life cycle. These states
are defined by the Thread.State enum, and they are as follows:
1. NEW: A thread is in the NEW state when it has been created but has not yet
started running.
2. RUNNABLE: A thread is in the RUNNABLE state when it has been started and is
either running or waiting to be scheduled to run by the operating system.
3. BLOCKED: A thread is in the BLOCKED state when it is waiting for a monitor
lock to be released so that it can enter a synchronized block or method.
4. WAITING: A thread is in the WAITING state when it is waiting for a specific
condition to be met before it can continue running. For example, a thread
might wait for another thread to complete its work before it can continue.
5. TIMED_WAITING: A thread is in the TIMED_WAITING state when it is waiting for
a specific period of time before it can continue running. For example, a
thread might sleep for a specified amount of time before continuing.
6. TERMINATED: A thread is in the TERMINATED state when it has completed its
work and has either been terminated or has naturally terminated.
1. NEW: A thread is created using the new keyword and is in the NEW state.
2. RUNNABLE: The start() method is called on the thread, and it becomes
eligible for scheduling by the operating system. It transitions to the
RUNNABLE state.
3. RUNNING: The operating system schedules the thread to run, and it
transitions to the RUNNING state.
4. BLOCKED or WAITING or TIMED_WAITING: While running, a thread may
transition to one of these states depending on the conditions it
encounters. For example, if it enters a synchronized block or method, it
may become BLOCKED. If it calls the wait() method, it becomes WAITING,
and if it calls the sleep() method, it becomes TIMED_WAITING.
5. RUNNABLE: When the blocking condition is resolved or the specified wait
time elapses, the thread becomes eligible for scheduling again and
transitions back to the RUNNABLE state.
6. TERMINATED: The thread completes its work and either terminates naturally
or is terminated explicitly using the stop() method. It transitions to the
TERMINATED state.
It's worth noting that threads can transition between states multiple times
during their life cycle, depending on the conditions they encounter while
running.