Results 1 to 3 of 3
  1. #1
    robo_ky is offline Member
    Join Date
    Apr 2011
    Posts
    2
    Rep Power
    0

    Default 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);
        	
        }
    }
    In this Exam , class DeprecateSuspendResume have two instance variables are
    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();
    and the result is shown in the console depend on the random() value of the time sleep of the main Thread
    Java Code:
    long time=(long)(Math.random()*2000.0);
        		System.out.println(time);
        		Thread.sleep(time);
    anyone can explan for me:
    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.

  2. #2
    Hibernate's Avatar
    Hibernate is offline Senior Member
    Join Date
    Dec 2010
    Location
    Stockholm, Sweden
    Posts
    222
    Blog Entries
    9
    Rep Power
    5

    Default

    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.

  3. #3
    Hibernate's Avatar
    Hibernate is offline Senior Member
    Join Date
    Dec 2010
    Location
    Stockholm, Sweden
    Posts
    222
    Blog Entries
    9
    Rep Power
    5

    Default

    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

  1. Hibernate/Suspend/PowerManagement
    By narthir in forum New To Java
    Replies: 0
    Last Post: 02-09-2011, 10:50 PM
  2. Can we use suspend() in jdk1.6 ?
    By makpandian in forum Threads and Synchronization
    Replies: 1
    Last Post: 10-26-2010, 03:23 PM
  3. suspend a method
    By mtz1406 in forum Threads and Synchronization
    Replies: 6
    Last Post: 10-21-2009, 09:36 PM
  4. Trying to get the resume verbage right...
    By Hagrid1960 in forum Jobs Discussion
    Replies: 0
    Last Post: 01-16-2009, 07:35 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •