• 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Concurrent threads

 
Ranch Hand
Posts: 175
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi!
I want to create two threads which run concurrently and I tried the following piece of code but when I run it the first thread executes first and then the second one runs. Can you point out where I'm going wrong?
Thanks,
Sasikanth.
public class Threads3{
public static void main(String args[]){
new th1();
new th2();
}
}
class th1 extends Thread{
public th1(){
System.out.println("creating an object of th1 class."+this);
start();
}
public void run()
{
for(int i=0;i<10;i++)
System.out.println("In thread1");
try{Thread.sleep(500);}
catch(InterruptedException e){System.out.println(e);}
}
}
class th2 extends Thread{
public th2(){
System.out.println("creating an object of th2 class."+this);
start();
}
public void run()
{
for(int i=0;i<10;i++)
System.out.println("In thread2");
try{Thread.sleep(500);}
catch(InterruptedException e){System.out.println(e);}
}
}
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The code after your for loop constructor is not inside curly braces. This means the next line
System.out.println("In thread1");
is printing 10 times and then the try block starts.
I put curly braces in and it works concurrently:
public class Threads3{
public static void main(String args[]){
new th1();
new th2();
}
}
class th1 extends Thread{
public th1(){
System.out.println("creating an object of th1 class."+this);
start();
}
public void run()
{
for(int i=0;i<10;i++) {
System.out.println("In thread1");
try{Thread.sleep(500);}
catch(InterruptedException e){System.out.println(e);}
}
}
}
class th2 extends Thread{
public th2(){
System.out.println("creating an object of th2 class."+this);
start();
}
public void run()
{
for(int i=0;i<10;i++) {
System.out.println("In thread2");
try{Thread.sleep(500);}
catch(InterruptedException e){System.out.println(e);}
}
}
}

[This message has been edited by j_mcd (edited March 21, 2001).]
 
Sasikanth Malladi
Ranch Hand
Posts: 175
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot, j_mcd!
Sasikanth
 
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
j_mcd
Please read the JavaRanch Name Policy and re-register using a name that complies with the rules.
Thanks for you cooperation.
------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform
 
Always! Wait. Never. Shut up. Look at this tiny ad.
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic