xyz
Answered On : Nov 27th, 2012
Code
{
public static void main
(String[] args
)
{
Thread t1
= new test
() {public void run
()
{
for (int i=0; i <=10;i++){
System.
out.
println(this.
getName());
try {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}};
// t1.setName("Thread 1");
t1.setPriority(2);
Thread t2
= new test
(){public void run
()
{
for (int i=0; i <=10;i++)
{
System.
out.
println(this.
getName());
try {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}};
t2.setPriority(2);
Thread t3
= new test
(){public void run
()
{
for (int i=0; i <=10;i++)
{
System.
out.
println(this.
getName());
try {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}};
// t3.setName("Thread 3");
t2.setPriority(2);
Thread t4
= new test
(){public void run
()
{
for (int i=0; i <=10;i++)
{
System.
out.
println(this.
getName());
try {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}};
// t4.setName("Thread 4");
t4.setPriority(2);
t1.start();
t2.start();
t3.start();
t4.start();
}
}
Login to rate this answer.
Amit
Answered On : Dec 7th, 2012
You can use JOIN() method to execute them one after another.
e.g :- t1.start();
t1.join();
t2.start();
t2.join();
Login to rate this answer.
Praveen Kumar
Answered On : Dec 13th, 2012
There is no way to determine which thread is executed first, it is solely dependent on the scheduling algorithms adapted by the operating system and underlying hardware architecture, one way to guarantee the that the thread runs in a particular order is using synchronized methods.
Login to rate this answer.
We have to implement run method to create a thread.We need to make that method synchronized so that only one thread can go inside and control will be given to second only and only of first is completed.
Login to rate this answer.