• 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

How the Threads work?

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Pleas analysis the results and explain how the two threads works:
public class ThreadTester {
public static void main(String args[]) {
HelloRunner hr=new HelloRunner();
Thread t1=new Thread(hr);
Thread t2=new Thread(hr);
t1.start();
t2.start();
}
}
class HelloRunner implements Runnable {
int i;
public void run() {
i=0;
while (true) {
System.out.println("Hello "+i++);
if (i==500) {
break;
}
}
}
}

[This message has been edited by fengzixuan zheng (edited October 14, 2001).]
 
Ranch Hand
Posts: 464
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by fengzixuan zheng:
Pleas analysis the results and explain how the two threads works:
public class ThreadTester {
public static void main(String args[]) {
HelloRunner hr=new HelloRunner();
Thread t1=new Thread(r);
Thread t2=new Thread(r);
}
}
class HelloRunner implements Runnable {
int i;
public void run() {
i=0;
while (true) {
System.out.println("Hello "+i++);
if (i==500) {
break;
}
}
}
}


Thread t1=new Thread(r);
Thread t2=new Thread(r);
Its should be hr not r
These threads havent been started yet
So start the threads also
This code will produce unpredictable results since its OS dependent
So inside run, when the infinite loop hits 500, it will breakout and exit
 
Ranch Hand
Posts: 317
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, Fengzixuan:
Your question is not very clear. You might want to ask question like the following code(I modified something):

If so, the programme will start two threads t1 and t2 and runs with result that is platform-dependent(print the number in a way that is not fixed).
If you define the run method as synchronised, the result will be predictable since the thread t1 will start first and lock the object HelloRunner when printing those numbers till it finished. Then thread t2 starts to print the number.
Hope it helps.
guoqiao

------------------
Guoqiao Sun
Sun Certified Programmer for Java™ 2 Platform
try my mock exam¹²³ at my homepage.
 
Destiny's powerful hand has made the bed of my future. And this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic