MultiThreading - Example 1
class aaa implements Runnable
{
aaa() // Default constructor
{
Thread t=new Thread(this);
t.start();
}
public void run()
{
for (int e=1; e<=10; e++)
{
System.out.println(e+"");
}
}
}
class psp
{
public static void main(String gg[])
{
aaa a=new aaa();
for(int f=201;f<=210;f++)
{
System.out.println(f+"");
}
}
}
Program details:-
1. In this program, Two threads will be : -
a) Main Thread, b) Separate Thread
2. Each & every lines of main fuction and also the methods are call from main are loaded on Main Thread.
3. Codes of default constructor are also loaded on Main Thread because that is called from main.
4. this pointer contains address of aaa's object and data type of this pointer is aaa.
5.start method is also loaded on Main Thread because that is called from default constructor and default constructor is called from main.
6.But run method is not called from start or start has some codes to pick run on Separate Thread. Thus run method is loaded on Separate Thread.
7.And in this program (Thread t;) there thread is predefined class.
whose pointer t is used to store the address of thread's object.
-----------------------------------------------------------------------------
MultiThreading Example - 2
class aaa extends Thread
{
aaa()
{
start();
}
public void run()
{
for(int e=1;e<=10;e++)
{
System.out.println(e+"");
}
}
}
class psp
{
public static void main(String gg[])
{
aaa a=new aaa();
for(int f=301;f<=310;f++)
{
System.out.println(f+"");
}
}
}
-------------------------------------------------------------------------------
No comments:
Post a Comment