• 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

Thread problem

 
Ranch Hand
Posts: 167
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
After a thread starts, do we still have the control?
class A implements Runable{
public int i=0;
public void run(){
try{
sleep(1000000)// 1 second
}catch interuptedException{}
i = 10;
}
}
public B{
public static void main(String args[]){
A a = new A();
Thread thread = new Thread(a);
thread.start()
// here
System.out.println("a.i="+a.i)
}
}
The above program, replace "// here" as one line of code, after run class B, it will print out "a.i=10"(not a.i=10!)
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why would you give us code with 13 errors in it? :P
You said: It will print out "a.i=10"(not a.i=10!)
Umm, it prints out 0 when I run my fixed up code. The thread is sleeping while the main process thread prints out the value. If you made the main process sleep 2 seconds before printing the value of i, you'd likely see the result of 10. Anyone agree?
class A implements Runnable{
  public int i=0;
  public void run() {
    try {
      Thread.currentThread().sleep(1000);
    }
    catch (Exception e) {}
    i = 10;
  }
}
public class B{
  public static void main(String args[]){
    A a = new A();
    Thread thread = new Thread(a);
    thread.start();
    System.out.println("a.i="+a.i);
  }
}
- Dave
 
nan sh
Ranch Hand
Posts: 167
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What I mean is that what kind of code I should replace "//here", then we can get o/p as "a.i=10".
One way is replace with a.i=10, but that is not our purpose,thread should set i = 10, how can we let thread does a.i=10 for us?
I don't know the answer, or maybe there is no answer, the question just come to my mind.
 
Dave Terrian
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How about:
try {thread.join();} catch (InterruptedException e) {}
 
nan sh
Ranch Hand
Posts: 167
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Dave,
I think that is right, I never knew the existing of this method, may I ask what is the difference between join() and sleep()?
 
Dave Terrian
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh I'm not a thread expert by any means.
In my simplistic view of the world sleep will pause a thread (move it from a running to a blocking state) until a specified time has elapsed.
join will also pause a thread but it is used to block as you wait for another thread to end (move to a dead state). There's also a combo variation that will pause while you wait on another thread to die but after a maximum duration, the thread moves back to a runnable state.
In other words, sleep is used if you just want to wait for a while and join is used if you started another thread to do some processing and you don't want to continue until that thread is done.
You might use sleep if you want to show a splash screen when your application starts up. You might use join if you started a printjob thread and one that spins an hourglass cursor and you want the main process thread to wait until the printjob finishes to signal the hourglass to stop.
There's also a yield method that puts your thread back into the ready state - essentially giving up the current running state and signaling to the scheduler that another ready-thread has the chance to jump in (if there is one).
Corrections or additions are welcome,
- Dave
 
Eat that pie! EAT IT! Now read this tiny ad. READ IT!
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic