Results 1 to 3 of 3

Thread: Exchanger Class

  1. #1
    allaudin's Avatar
    allaudin is offline Senior Member
    Join Date
    Apr 2013
    Posts
    271
    Rep Power
    0

    Default Exchanger Class

    Hy everybody, please have a look why this code doesn't show any output ... thanks in advance
    Java Code:
    /* ---- Exchanger Class Demo ---*/
    
    import java.util.concurrent.Exchanger;
    
    class MakeString implements Runnable{
    		Exchanger<String> exg;
    		String str;
    		
    		public MakeString(Exchanger<String> exg){
    			
    			this.exg = exg;
    			str = new String();
    			new Thread(this).start();
    		}
    		
    		public void run(){
    			char c = 'A';
    			
    			for(int i = 0; i < 3; i++){
    				
    				for(int j = 0; i < 5; j++) str += c++;
    					try{
    						str = exg.exchange(str);  /* exchange with empty string */
    						}catch(InterruptedException IE){
    							System.err.println("Thread Interrupted");
    					}
    			
    			}
    		}
    }
    
    class UseString implements Runnable{
    	
    	Exchanger<String> exg;
    	String str;
    	
    	public UseString(Exchanger<String> exg){
    		this.exg = exg;
    		new Thread(this).start();
    	}
    	
    	public void run(){
    		
    		try{
    				for(int i = 0; i < 3; i++){
    						str = exg.exchange(new String());
    						System.out.println("GOT: " + str);
    				}
    		}catch(InterruptedException ie){
    			System.err.println("Thread Interrupted");
    		}
    	}
    
    }
    public class ExchangerApp{
    
    	public static void main(String[] args){
    			Exchanger<String> exg = new Exchanger<>();
    			new UseString(exg);
    			new MakeString(exg);
    	}
    }

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,411
    Rep Power
    25

    Default Re: Exchanger Class

    why this code doesn't show any output
    That is usually because no print statements are executed. Often that is because of logic problems.

    Try debugging the by adding some println() statements to print out the values of variables as the code executes so you can see what the computer sees when it executes and know why it is doing what it is doing when the code executes.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    allaudin's Avatar
    allaudin is offline Senior Member
    Join Date
    Apr 2013
    Posts
    271
    Rep Power
    0

Similar Threads

  1. Replies: 3
    Last Post: 12-08-2013, 09:28 PM
  2. Replies: 1
    Last Post: 12-13-2012, 03:06 AM
  3. Replies: 1
    Last Post: 11-29-2012, 09:46 AM
  4. Replies: 0
    Last Post: 12-07-2011, 11:55 AM
  5. Replies: 0
    Last Post: 03-27-2011, 05:49 AM

Posting Permissions

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