Content-Length: 3027333 | pFad | https://www.scribd.com/document/681710912/Multi-Threading
2Multi Threading
Multi Threading
Multi Threading
Threads are light weight process because they utilize minimum resources
of the system. This means they takes less memory and less processor time.
1) User Thread :User threads are threads which are created by the application
or user. They are high priority threads. JVM (Java Virtual Machine) will not exit
until all user threads finish their execution. JVM wait for these threads to
finish their task. These threads are foreground threads.
1. New
2. Runnable
3. Running
4. Non-Runnable /Blocked/Waiting
5. Terminated/Dead
1) New:The thread is in new state if you create an instance of Thread class but
before the invocation of start() method.
Step 1:You will need to override run( ) method available in Thread class. This
method provides an entry point for the thread and you will put your complete
business logic inside this method. Following is a simple syntax of run() method
–public void run( )
Step 2:Once Thread object is created, you can start it by calling start() method,
which executes a call to run( ) method. Following is a simple syntax of start()
method −
void start( );
T1.start();
T2.start();
Output:
Thread Methods:
Following is the list of important methods available in the Thread class.
public void start():starts the execution of the thread, then invokes the run()
method on this Thread object.
Sleep(int time);
This is the another method to create thread. We create a new class which
implements java.lang.Runnable interface and override run() method. Then we
instantiate a Thread object and call start() method on this object.
}
classThreadB implements Runnable
{
public void run()
{
System.out.println("Running " + Thread.currentThread().getName());
for(int i=6; i<=10; i++)
{
System.out.println(Thread.currentThread().getName()+ "=" + i);
}
System.out.println(Thread.currentThread().getName() + " exiting.");
}
}
Output:
Priority of a Thread (Thread Priority):
In a Multi threading environment, thread scheduler assigns processor to
a thread based on priority of thread. Whenever we create a thread in Java, it
always has some priority assigned to it. Priority can either be given by JVM
while creating the thread or it can be given by programmer explicitly.
System.out.println("Thread Running..."+Thread.currentThread().getName());
t1.start();
t2.start();
t3.start();
Output:
Example : Set Priority
}
}
Output:
Stopping and Blocking a thread:
Java Synchronization is better option where we want to allow only one thread
to access the shared resource at a given point in time.
class First
{
public void display(String msg)
{
System.out.print ("["+msg);
try
{
Thread.sleep(1000);
}
catch(InterruptedException e)
{
e.printStackTrace();
}
System.out.println ("]");
}
}
Syntax:
class First
{
synchronized public void display(String msg)
{
System.out.print ("["+msg);
try
{
Thread.sleep(1000);
}
catch(InterruptedException e)
{
e.printStackTrace();
}
System.out.println ("]");
}
}
Output:
[welcome]
[programmer]
[new]
2. Using Synchronized Block.
If want to synchronize access to an object of a class or only a part of a method
to be synchronized then we can use synchronized block for it. It is capable to
make any part of the object and method synchronized.
Example
In this example, we are using synchronized block that will make the display
method available for single thread at a time.
class First
{
public void display(String msg)
{
System.out.print ("["+msg);
try
{
Thread.sleep(1000);
}
catch(InterruptedException e)
{
e.printStackTrace();
}
System.out.println ("]");
}
}
Output:
[welcome]
[new]
[programmer]
Fetched URL: https://www.scribd.com/document/681710912/Multi-Threading
Alternative Proxies: