Results 1 to 3 of 3
Thread: problem with suspend and resume
- 04-28-2011, 05:29 AM #1
Member
- Join Date
- Apr 2011
- Posts
- 2
- Rep Power
- 0
problem with suspend and resume
I'm begining with Thread ,and when I read the exam about resume and suspend
I can't understand
this is the exam:
PHP Code:import java.lang.Thread; public class DeprecateSuspendResume extends Object implements Runnable { public volatile int first; public int second; public boolean equal() { return(first==second); } public void run() { try { first=0; second=0; work(); } catch(InterruptedException e) { } } public void work()throws InterruptedException { int val=1; while(true) { step1(val); step2(val); Thread.sleep(200); val++; } } public void step1(int val)throws InterruptedException { first=val; Thread.sleep(300); } public void step2(int val)throws InterruptedException { second=val; } public static void main(String[] args)throws InterruptedException { DeprecateSuspendResume th=new DeprecateSuspendResume(); Thread t=new Thread(th); Thread.sleep(1000); t.start(); for(int i=0;i<10;i++) { t.suspend(); System.out.println(th.equal()+" time:"); t.resume(); long time=(long)(Math.random()*2000.0); System.out.println(time); Thread.sleep(time); } System.exit(0); } }
first and second
Our task is check whether first== second or not
in main method Thread main must be pause for 1 second to t thread init and run
PHP Code:Thread.sleep(1000); t.start();
Java Code:long time=(long)(Math.random()*2000.0); System.out.println(time); Thread.sleep(time);
1)
why we must be pause the main thread before t thread init and run ?
the code will ok without it
2)
why the result(true or false) must depend on the random value of the time sleep of the main thread
thank so much!Last edited by robo_ky; 04-28-2011 at 05:32 AM.
- 08-09-2011, 12:30 AM #2
- Join Date
- Dec 2010
- Location
- Stockholm, Sweden
- Posts
- 222
- Blog Entries
- 9
- Rep Power
- 11
I did not read your post (it is just too long for me for the moment), but I read your title.
But suspend and resume are deprecated because the make the program behave arbitrarily, due to some resource issues.
They are however usable if you use them perfectly, but that is not always so easy, and it should not be done, even if I do it some times.
And now I will attempt to read your post...
Edit:
Well, at first gaze it appears as if you do know they are not to be used... maybe you should have added another word in the end of the title.Last edited by Hibernate; 08-09-2011 at 12:33 AM.
Ex animo! Hibernate
Java, Arch Linux, C, GPL v3, Bash, Eclipse, Linux VT, GNOME 2 and many buttons on windows.
- 08-09-2011, 12:44 AM #3
- Join Date
- Dec 2010
- Location
- Stockholm, Sweden
- Posts
- 222
- Blog Entries
- 9
- Rep Power
- 11
Know idea about what you mean with the first question, but one question two (and you have probably already done the exam, but I will not give you direct answer.):
Have you tested not sleeping in step1()?
What is volatile?
How much must/can you sleep in main()?
How do a sleeping thread behave if it is suspended while sleeping?
Is the phase of the moon a parameter (it is sometimes)?Ex animo! Hibernate
Java, Arch Linux, C, GPL v3, Bash, Eclipse, Linux VT, GNOME 2 and many buttons on windows.
Similar Threads
-
Hibernate/Suspend/PowerManagement
By narthir in forum New To JavaReplies: 0Last Post: 02-09-2011, 10:50 PM -
Can we use suspend() in jdk1.6 ?
By makpandian in forum Threads and SynchronizationReplies: 1Last Post: 10-26-2010, 03:23 PM -
suspend a method
By mtz1406 in forum Threads and SynchronizationReplies: 6Last Post: 10-21-2009, 09:36 PM -
Trying to get the resume verbage right...
By Hagrid1960 in forum Jobs DiscussionReplies: 0Last Post: 01-16-2009, 07:35 PM
Bookmarks