Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: unable to understand that how control transfer in this program .

  1. #1
    Junior Member
    Join Date
    Feb 2014
    Posts
    23
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default unable to understand that how control transfer in this program .

    public class SRDemo extends Thread { 
    	public void run() { 
    		try { 
    			for( int i = 0; i < 7; i++ ) { 
    				Thread.sleep(500); 	
    				System.out.println( this.getName() + ": " + i );
    			}
    		}	
    		catch( InterruptedException e ) { 
    		e.printStackTrace(); 
    		} 
    	} 
    	public static void main( String args[ ] ) { 
    		SRDemo srd1 = new SRDemo(); 
    		SRDemo srd2 = new SRDemo(); 
    		srd1.setName("First"); 
    		srd2.setName("Second");
    		srd1.start(); 
    		srd2.start(); 
    		try { 
    			Thread.sleep( 1000 ); 
    			srd1.suspend();
    			System.out.println("Suspending thread First");
    			Thread.sleep( 1000 ); 
    			srd1.resume(); 
    			System.out.println("Resuming thread First");
     
    			Thread.sleep(1000); 
    			srd2.suspend(); 
    			System.out.println("Suspending thread Second");
    			Thread.sleep(1000); 
    			srd2.resume();
    			System.out.println("Resuming thread Second");
    		} 
     
    		catch(InterruptedException e) { 
    			e.printStackTrace(); 
    		} 
    	} 
    }

    I am not understanding about the control ..that how control transfer from one block to another especialy in thread programing
    and one more thing please explain me when the try{} block of main() function get excuted..


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,751
    My Mood
    Mellow
    Thanks
    233
    Thanked 711 Times in 697 Posts

    Default Re: unable to understand that how control transfer in this program .

    You have 3 threads running: the main() thread, srd1, and srd2. (There are also other threads running, but you can ignore those for now.) Each of the 3 threads of interest are pretty much off doing their own thing, so I'm not sure what you mean by "control transfer from one block to another." Think of it as 3 tasks, each executing on its own, and see if that helps.

    Of course one of the threads has some control over the other two, AND the other two are both doing the same task, but that helps demonstrate how the 3 threads are related while they perform their tasks.

    Though your use of deprecated methods simplifies this little demo, don't use suspend() and resume() in a real project.

  3. The Following User Says Thank You to GregBrannon For This Useful Post:

    gautammuktsar@gmail.com (March 12th, 2014)

Similar Threads

  1. program to transfer files from one server to another
    By trisha mehta in forum What's Wrong With My Code?
    Replies: 0
    Last Post: June 5th, 2013, 12:54 AM
  2. Replies: 19
    Last Post: March 7th, 2012, 07:26 PM
  3. Replies: 13
    Last Post: December 1st, 2011, 10:36 PM
  4. Simplest Problem but i am unable to understand it, WHY???
    By Mr.777 in forum Object Oriented Programming
    Replies: 11
    Last Post: October 26th, 2011, 03:28 AM
  5. Program with transfer value from one class to another
    By Adam22 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: January 5th, 2011, 11:26 PM