Creation of thread by implementing the Runnable interface
Approach 1
class NewThread implements Runnable
{
Thread t;
NewThread()
{
t = new Thread(this); //(1)
[Link](“Child Thread is born”);
[Link]();
[Link](“Child Thread enters Runnable state”);
}
public void run()
{
[Link](“Child Thread enters Running state”);
for(int i=10; i>0; i--)
{
[Link](“Child Thread:”+i);
}
[Link](“Exit from Child Thread”);
}
}
class ThreadDemo
{
public static void main(String[] args)
{
[Link](“main Thread begins”)
new NewThread(); //(2)
[Link](“main Thread ends”)
}
}
Note:
(1) An object of Thread class is created using the Thread(Runnable target) constructor
where:
target is the object of a class (here NewThread, by using this keyword) that
implements the Runnable interface, whose run() method is called when the Thread
object constructed by this constructor gets its own run() method invoked by the JRE.
(2) An object of NewThread class is created using the NewThread()constructor which in
turn creates the Thread object and starts it up.
Approach 2
class NewThread implements Runnable
{
public void run()
{
[Link](“Child Thread enters Running state”);
for(int i=10; i>0; i--)
{
[Link](“Child Thread:”+i);
}
[Link](“Exit from Child Thread”);
}
}
class ThreadDemo
{
public static void main(String[] args)
{
[Link](“main Thread begins”)
NewThread nt = new NewThread();
Thread t = new Thread(nt);
[Link](“Child Thread is born”);
[Link]();
[Link](“Child Thread enters Runnable state”);
[Link](“main Thread ends”)
}
}
Creation of thread by extending the Thread class
Approach 1
class NewThread extends Thread
{
NewThread()
{
[Link](“Child Thread is born”);
start();
[Link](“Child Thread enters Runnable state”);
}
public void run()
{
[Link](“Child Thread enters Running state”);
for(int i=10; i>0; i--)
{
[Link](“Child Thread:”+i);
}
[Link](“Exit from Child Thread”);
}
}
class ThreadDemo
{
public static void main(String[] args)
{
[Link](“main Thread begins”);
new NewThread();
[Link](“main Thread ends”);
}
}
Approach 2
class NewThread extends Thread
{
public void run()
{
[Link](“Child Thread enters Running state”);
for(int i=10; i>0; i--)
{
[Link](“Child Thread:”+i);
}
[Link](“Exit from Child Thread”);
}
}
class ThreadDemo
{
public static void main(String[] args)
{
[Link](“main Thread begins”);
NewThread nt = new NewThread();
[Link](“Child Thread is born”);
[Link]();
[Link](“Child Thread enters Runnable state”);
[Link](“main Thread ends”);
}
}
A simple example of multithreaded programming
class A extends Thread
{
public void run()
{
for(int i=1; i<=25; i++)
{
[Link]("\tFrom Thread A: i = "+i);
}
[Link]("Exit from A");
}
}
class B extends Thread
{
public void run()
{
for(int j=1; j<=25; j++)
{
[Link]("\tFrom Thread B: j = "+j);
}
[Link]("Exit from B");
}
}
class C extends Thread
{
public void run()
{
for(int k=1; k<=25; k++)
{
[Link]("\tFrom Thread C: k = "+k);
}
[Link]("Exit from C");
}
}
class ThreadDemo
{
public static void main(String[] args)
{
[Link](“main Thread begins”);
A thA = new A();
[Link]();
B thB = new B();
[Link]();
C thC = new C();
[Link]();
[Link](“main Thread ends”);
}
}
An example of multithreaded programming involving various Thread
class methods
class A extends Thread
{
public void run()
{
for(int i=1; i<=15; i++)
{
if(i==3) yield();
[Link]("\tFrom Thread A: i = "+i);
}
[Link]("Exit from A");
}
}
class B extends Thread
{
public void run()
{
for(int j=1; j<=5; j++)
{
[Link]("\tFrom Thread B: j = "+j);
if(j==3) stop();
}
[Link]("Exit from B");
}
}
class C extends Thread
{
public void run()
{
for(int k=1; k<=5; k++)
{
[Link]("\tFrom Thread C: k = "+k);
try
{
if(k==3)
sleep(10000);
}
catch(InterruptedException ie)
{
}
}
[Link]("Exit from C");
}
}
class ThreadDemo
{
public static void main(String[] args)
{
A a = new A();
B b = new B();
C c = new C();
[Link]("Start thread A");
[Link]();
[Link]("Start thread B");
[Link]();
[Link]("Start thread C");
[Link]();
[Link]("End of main thread");
}
}