• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Threads & synchronization

 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In this code Should the output be
One
JAVARANCH
Two
JAVARANCH
But the o/p you get is
One
Two
JAVARANCH
JAVARANCH
Why is that so? So that menas two threads running at the same time?
Can anybody clear my doubt.
BTB this program was put on this forom itself by somebodyelse,I just wanted to find out which thread runs first.

Thanks
sudha
public class Tes1 extends Thread {
public Tes1(String s){

super(s);
}
public void run() {

print();
}
public synchronized void print() {
System.out.println("JAVARANCH");
try {
Thread.sleep(5000);
}
catch(InterruptedException ie) {
System.out.println(ie.getMessage());
}
}
public static void main(String arg[]) {
Tes1 t1 = new Tes1("One");
Tes1 t2 = new Tes1("Two");
System.out.println(t1.getName());
t1.start();
System.out.println(t2.getName());
t2.start();
}
}
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sudha,
Following is my View regarding your doubt:
Let us analyse following four lines of code.
public static void main(String arg[]) {
// Following two lines, u are creating thread
Tes1 t1 = new Tes1("One");
Tes1 t2 = new Tes1("Two");
// When executing the following line, the "One" is printed
System.out.println(t1.getName());
// When executing the following line, your thread t1 first makes its entry with Thread Schedular and then state called "Eligible for run". And now your thread is competing for CPU time to execute. It does not mean that the moment you execute t1.start(), your thread has to execute. Before your thread get the time to execute, current thread (ie main method, it is also thread, goes to next lines of code to execute. Hence your output comes like this. It is dependent on CPU and how it schedules the thread.
t1.start();
System.out.println(t2.getName());
t2.start();
}
I hope this solves, your doubt. If i am wrong, please correct me.
You can mail me to sakthima@rediffmail.com
Thanks
Sakthivel.
 
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In a nutshell the reason the output is:
One
Two
JAVARANCH
JAVARANCH
Has to due with the fact that once the start() is called the Main Thread returns right back to the Main Method and continues running. The start() method calls the run() method but exactly when is determined by JVM.
If you want to make life more interesting try using the following:
t1.start();
System.out.println(t1.getName());
System.out.println(t2.getName());
t2.start();
Can you predict what the output will be?
Regards,
Travis M. Gibson
(Future SCJP - Nov. 19, 2000)
 
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You might want to read the discussion on a similar topic here.
Ajith
 
Sudha Kris
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks everybody.
So that means eventhough you start 2 threads there's no guarantee that which one will execute first. Is it the same case even if one thread has higher priority than the other.
I'll try it out
Ok,
Thanks
Sudha
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can tell like both (threads 1 and 2 ) are acquire different object(object for thread 1 and objects for thread 2).
so each one can run its own object.
same time mail tread also running in parallel so u get such a output

hope it helps..
 
reply
    Bookmark Topic Watch Topic
  • New Topic