Multi Threading - Java

Java supports for multithreaded programming.amutithreaded program contains two or more parts that can run concurrently.each part of such a program is called athread. And each theread defines a spearte path of execution. Thus multithreading is  a specified form of multitasking.
There are two two distinct types of multitasking:

            Ex: you can open notepad , at the same time you can compile the java program

 

            Multithreading enables you to write very efficient programs that may be maximum use of the cpu.
The java’s multithreading system is built upon the thread class.its methods & companion interface runnable.the thread class defines several methods that helps to  manage threads.

getname( )      to obtain a thread name.
getpriority( )   to obtain a thread name.
isalive( )          determines if a thread is still running.
join( )              wait for thread to terminate.
run( )              entry point for thread.
sleep( )            suspend athread for aperiod of time
start( )             start athread by calling its run( ).

 Main Thread:

                        When java program start up one thread begins running immediately.This is usually called the mainthread of your program the main thread is  important for two reasons.

            It is the thread for which other child thread will be spawned.
It must be the last thread to finish execution when the main thread stops, your program terminates.

The easiest way to create a thread is to  implement the runnable interface.Runnable extracts aunit  of executable code to implement runnable. A class need only implement a single method called run( ). the thread class has two constructors.

WRITE A PROGRAM TO PRINT THE NUMBERS USING THE RUNNABLE INTERFACE.

            class Abc implements Runnable
{
Thread t=null;
Abc()
{
t=new Thread(this,"Child");
t.start();
}

public void run()
{
try
{
for(int i=1;i<=5;i++)
{
System.out.println("Child "+i);
Thread.sleep(500);
}
}
catch(InterruptedException e)
{
System.out.println("Distributed by user");
}
System.out.println("normal out of child");
}
}
class Sample
{
public static void main(String args[])
{
Abc x=new Abc();
System.out.println("abc.x");
try
{
for(int i=1;i<=5;i++)
{
System.out.println("Main"+i)
Thread.sleep(1000);
}
}
catch(InterruptedException e)
{
System.out.println("INterrupted"+e);
}
System.out.println("Normal Out of main");
}
}

WRITE A PROGRAM ON EXTENDING THREAD CLASS

class Abc extends Thread
{
Abc()
{
super("Child");
System.out.println(this);
start();
}
public void run()
{
try
{
for(int i=1;i<=5;i++)
{
System.out.println("Child"+i);
 Thread.sleep(500);
}
}
catch(InterruptedException e)
{
System.out.println("INterrupted"+e);
}
System.out.println("NOrmal Out of main");
}
}

 

class Sample
{
public static void main(String args[])
{
new Abc();
try
{
for(int i=1;i<=5;i++)
{
System.out.println("Main"+i);
Thread.sleep(1000);
}
}
catch(InterruptedException e)
{
System.out.println("Distributed by user");
}
System.out.println("Normal out of main");
}
}

WRITE A PROGRAM ON MULTIPLE THREADS

class Abc implements Runnable
{
Thread t;
String k;
Abc(String s)
{
k=s;
t=new Thread(this,s);
System.out.println(t);
t.start();
}
public void run()
{

 

                try
{
for(int i=3;i>=0;i--)
{
System.out.println(k+":"+i);
Thread.sleep(1000);
}
}
catch(InterruptedException e)
{
System.out.println("Distrubed by user");
}
System.out.println("Normal out of");
}
}
class Sample
{
public static void main(String args[])
{
new Abc("one");
new Abc("two");
new Abc("three");
try
{
Thread.sleep(10000);
}
catch(InterruptedException e)
{
System.out.println("Interrupted");
}
System.out.println("Normal out of main");
}
}

WRITE A PROGRAM ON ISALIVE AND JOIN.

class Abc implements Runnable
{
Thread t;
String k;
Abc(String s)
{
k=s;
t=new Thread(this,s);
System.out.println(t);
t.start();
}
public void run()
{
try
{
for(int i=3;i>=0;i--)
{
System.out.println(k+":"+i);
Thread.sleep(1000);
}
}

 

                catch(InterruptedException e)
{
System.out.println("Distrubed by user");
}
System.out.println("Normal out of");
}
}
class Sample
{
public static void main(String args[])
{
Abc a1=new Abc("one");
Abc a2=new Abc("two");
Abc a3=new Abc("three");
System.out.println(a1.t.isAlive());
System.out.println(a2.t.isAlive());
System.out.println(a3.t.isAlive());
try
{
System.out.println("Waiting thread to terminate");
a1.t.join();
a2.t.join();
a3.t.join();
}
catch(InterruptedException e)
{
System.out.println("Interrupted");
}
System.out.println(a1.t.isAlive());
System.out.println(a2.t.isAlive());
System.out.println(a3.t.isAlive());

 

        }
}


WRITE A PROGRAM ON THREAD PRIORITIES

class Abc implements Runnable
{
Thread t;
int c=0;
boolean b=true;
Abc(int s)
{
t=new Thread(this);
t.setPriority(s);
}
public void run()
{
while(b)
c++;
}

        public void stop()
{
b=false;
}
public void start()
{
t.start();
}
}
class Sample
{
public static void main(String args[])
{
Abc l=new Abc(Thread.NORM_PRIORITY-2);
Abc h=new Abc(Thread.NORM_PRIORITY+2);
l.start();
h.start();
try
{
Thread.sleep(10000);
}
catch(InterruptedException e)
{
System.out.println("Exception ");
}
l.stop();
h.stop();
try
{
l.t.join();
h.t.join();
}
catch(InterruptedException e)
{
System.out.println("Interruptrd ");
}
System.out.println("lOw "+l.c);
System.out.println("High "+h.c);
}
}

Sychronization:

                        When two or more thread need access to shared resources they need some way to ensure that the resources will be used by only one thread at a time. The process by which this is achieved is called synchronization.

WRITE A PROGRAM ON SYCHRONIZATION

class Abc
{
String s;
synchronized public void get(String k)
{
s=k;
System.out.println("["+k);
try
{
Thread.sleep(1000);
}
catch(InterruptedException e)
{
System.out.println("Exception"+e);
}
System.out.println("]");
}
}
class Xyz implements Runnable
{
Thread t;
String s;
Abc a1;
public Xyz(Abc a1,String k)
{
this.a1=a1;
s=k;
t=new Thread(this);
t.start();
}
public void run()
{
a1.get(s);
}
}

 

 

class sample
{
public static void main(String argc[])
{
Abc a=new Abc();
Xyz x1=new Xyz(a,"Hello");
Xyz x2=new Xyz(a,"Vision");
Xyz x3=new Xyz(a,"Computers");
try
{
x1.t.join();
x2.t.join();
x3.t.join();
}
catch(InterruptedException e)
{
System.out.println("Error"+e);
}
}
}

WRITE A PROGRAM ON SYCRONIZED BLOCK.

class Abc
{
String s;
public void get(String k)
{
s=k;
System.out.println("["+k);
try
{
Thread.sleep(1000);
}
catch(InterruptedException e)
{
System.out.println("Exception"+e);
}
System.out.println("]");
}
}
class Xyz implements Runnable
{
Thread t;
String s;
Abc a1;
public Xyz(Abc a1,String k)
{
this.a1=a1;
s=k;
t=new Thread(this);
t.start();
}
public void run()
{
synchronized(a1)
{
a1.get(s);
}
}
}
class Sample
{
public static void main(String argc[])
{
Abc a=new Abc();
Xyz x1=new Xyz(a,"Hello");
Xyz x2=new Xyz(a,"Vision");
Xyz x3=new Xyz(a,"Computers");
try
{
x1.t.join();
x2.t.join();
x3.t.join();
}
catch(InterruptedException e)
{
System.out.println("Error"+e);
}
}
}

Interthread communication:
 
                   Java includes an elegant inter process common mechanisn via the wait( ),   notify( ) and notify All( )methods.All these 3 methods can be called only from with in a synchronized method

wait:   
Tells the calling thread to give up the monitor and goto sleep until some other thread enters the same monitor and calls notify.

notify:

Wakes up the first thread that called wait( )on the same object.

notifyAll:

Wakes up all the threads that called wait( ) on the same object. the highest priority thread will run first.

WRITE A PRODUCER AND CONSUMER PROBLEM USING INTERTHREAD COMMUNICATION.

class Que
{
boolean r=false;
int n;
synchronized int get()
{
if(!r)
{
try
{
wait();
}
catch(Exception e)
{
System.out.println("Exception"+e);
}
r=false;
}
System.out.println("Get"+n);
notify();
return(n);
}
synchronized void put(int n)
{
if(r)
{
try
{
wait();
}
catch(Exception e)
{
System.out.println("Error:"+e);
}
}
this.n=n;
System.out.println("put"+n);
r=true;
notify();
}
}
class Producer implements Runnable
{
Que q;
producer(Que q)
{
this.q=q;
new Thread(this, "producer").start();
}
public void run()
{
int i=0;
while(true)
{
q.put(i++);
}
}
}
class Consumer implements Runnable
{
Que q;
consumer(Que e)
{
this.q=q;
new Thread(this, "Consumer").start();
}
public void run()
{
while(true)
{
q.get();
}
}
}
class Sample
{
public static void main(String args[])
{
Que q=new Que();
new Producer(q);
new Consumer(q);
System.out.println("^c to exit");
}
}

 

 

For
More Explanation
&
Online Classes

Contact Us:
+919885348743