MultiThreading and Synchronization
MultiThreading and Synchronization
Multithreaded Programming
A process is a program that is executing. Thus processbased multitasking is the feature that allows your
computer to run two or more programs concurrently.
Example process based multitasking enables you to run
the java compiler at the same time that you are using a
text editor. In process based multitasking, a program is
the smallest unit of code.
Thread states
Running
Ready
Suspended
Resumed
Blocked
terminated
synchronization
Messaging
Java provides a clean, low cost way for two or more threads to
communicate with each other via calls to predefined methods
that all object have.
Javas messaging system allows a thread to enter a
synchronized method on an object, and then wait there until
some other thread explicitly notifies it to come out.
Javas multithreading system is built upon the thread class, its methods, and its
companion interface, Runnable.
To create a new thread, your program will either extend Thread or implement the
Runnable interface.
The thread class defines several methods that help to manage threads.
Method
Meaning
getName
getPriority
isAlive
Join
Run
Sleep
Start
class Threadtest
{
public static void main(String args[])
{
new A() .start();
new B().start();
new C().start();
}
}
class ThreadMethods
{
public static void main(String args[])
{
A threadA=new A();
B threadB=new B();
C threadC=new C();
System.out.println("start thread A");
threadA.start();
System.out.println("start thread B");
threadB.start();
System.out.println("Start thread C");
threadC.start();
System.out.println("End of main thread");
}
class Threadtest
{
public static void main(String args[])
{
A threadA=new A();
B threadB= new B();
threadB.setPriority(Thread.MAX_PRIORITY);
threadA.setPriority(Thread.MIN_PRIORITY);
System.out.println(start thread A);
threadA.start();
System.out.println(start thread B);
threadB.start();
}
isAlive():
final boolean isAlive()
The isAlive() method returns true if the thread upon which it is
called is still running. It returns false otherwise.
join(): (wait for thread to terminate)
final void join() throws InterruptedException
This method waits until the thread on which it is called
terminates.
Example 3
class A extends Thread
{
public void run()
{ for(int i=1;i<5;i++){
System.out.println("\tFrom Thread A: i=" +i); }
System.out.println("Exit from A");
}
void call()
{ start(); } }
Example 3
class B extends Thread
{
public void run()
{
for(int j=1;j<5;j++)
{
System.out.println("\tFrom Thread B: j=" +j);
}
System.out.println("Exit from B");
}
}
Example 3
class ThreadMethods1
{
public static void main(String args[])
{ A threadA=new A();
B threadB=new B();
System.out.println("start thread A");
threadA.call();
System.out.println("start thread B");
threadB.start();
System.out.println("End of main thread");
}
}
Implementing Runnable
Steps:
1. Declare the class as implementing the Runnable
interface.
2. Implement the run() method.
3. Create a thread by defining an object that is
instantiated from this runnable class as the target of
the thread.
4. Call the threads start() method to run the method.
Example1(Runnable interface)
class runnableex implements Runnable
{
public String t;
runnableex(String s){t=s;}
public void run()
{
for(int i=1;i<=5;i++)
{
System.out.println(t+ "Thread " + i);
}
System.out.println("End of thread" + t);
}
}
class runnable1{
public static void main(String args[])
{
runnableex ob1=new runnableex("FIRST");
Thread thread1=new Thread(ob1);
runnableex ob2=new runnableex("SECOND");
Thread thread2=new Thread(ob2);
System.out.println("thread1");
thread1.start();
System.out.println("thread2");
thread2.start();
System.out.println("End of main");
}
}
Example1(Runnable interface)
class runnableex implements Runnable //step1
{
public String t;
runnableex(String s){t=s;}
public void run()
//step2
{
for(int i=1;i<=5;i++)
{
System.out.println(t+ "Thread " + i);
}
System.out.println("End of thread" + t);
}
}
class runnable1{
public static void main(String args[])
{
runnableex ob1=new runnableex("FIRST");
Thread thread1=new Thread(ob1); //step3
runnableex ob2=new runnableex("SECOND");
Thread thread2=new Thread(ob2);
System.out.println("thread1");
thread1.start();
//step4
System.out.println("thread2");
thread2.start();
System.out.println("End of main");
}
}
Example2(using Runnable
interface)
class runnableex2 implements Runnable
{
public String t;
runnableex2(String s){t=s;}
public void run()
{
for(int i=1;i<=5;i++)
{ System.out.println(t+ "Thread " + i); }
System.out.println("End of thread" + t);
}
void get1(Thread G)
{ G.start(); }
}
Example2(using Runnable
interface)
class runnable2{
public static void main(String args[])
{
runnableex2 ob1=new runnableex2("FIRST");
Thread thread1=new Thread(ob1);
runnableex2 ob2=new runnableex2("SECOND");
Thread thread2=new Thread(ob2);
System.out.println("thread1");
ob1.get1(thread1);
System.out.println("thread2");
ob2.get1(thread2);
System.out.println("End of main");
}
Synchronization
Example:
class callme{
void call(String msg){
System.out.print([ +msg);
try{
Thread.sleep(1000);
}catch(InterruptedException e){
System.out.println(interrupted);
}
System.out.println(]);
}
}
}
This prevents other threads from entering call()
while another thread is using it.
Deadlock
Thread A
Synchronized method2()
{
Synchronized method1()
{
.
.
}
}
Thread B
Synchronized method1()
{
Synchronized method2()
{