Results 1 to 4 of 4
  1. #1
    Andyj is offline Member
    Join Date
    Oct 2009
    Posts
    10
    Rep Power
    0

    Default Threading problem

    Hi there

    Working through Sierra & Bates for the SCJP, and stuck on one of the excercises. I think I'm missing something obvious here:

    I want three threads to modify my StringBuffer object so the result should be the letter A 100 times, B 100 times and C 100 times without interfering with eachother. I can't get the iteration to work though. Does anyone have any tips? Thanks so much! Andy


    class JenkSynch extends Thread{
    JenkSynch(StringBuffer y){}
    public void run(){
    synchronized (y){
    if (JenkSynch.currentThread().getName() == "Gavin")
    {y.append("B");
    y.delete(0,1);}
    if (JenkSynch.currentThread().getName() == "Howard")
    {y.append("C");
    y.delete(0,1);}
    for (int x = 0; x<100; x++) {
    System.out.print (y);}
    }

    }


    public static void main(String[] args){



    JenkSynch Sam = new JenkSynch(y);
    JenkSynch Gav = new JenkSynch(y);
    JenkSynch How = new JenkSynch(y);

    Gav.setName("Gavin");
    How.setName("Howard");

    Sam.start();
    Gav.start();
    How.start();
    }

    static StringBuffer y = new StringBuffer("A");

    }

  2. #2
    Andyj is offline Member
    Join Date
    Oct 2009
    Posts
    10
    Rep Power
    0

    Default

    Apologies, here it is again with code tags:

    Java Code:
     
    
    class JenkSynch extends Thread{
    	JenkSynch(StringBuffer y){}	
    		public void run(){
    			synchronized (y){
    				if (JenkSynch.currentThread().getName() == "Gavin")
    					{y.append("B");
    					y.delete(0,1);}
    				if (JenkSynch.currentThread().getName() == "Howard")
    					{y.append("C");
    					y.delete(0,1);}
    				for (int x = 0; x<100; x++) {
    						System.out.print (y);}
    						}
    						
    								}
    									public static void main(String[] args){
    
    	JenkSynch Sam = new JenkSynch(y);
    	JenkSynch Gav = new JenkSynch(y);
    	JenkSynch How = new JenkSynch(y);
    
    	Gav.setName("Gavin");
    	How.setName("Howard");
    
    	Sam.start();
    	Gav.start();
    	How.start();
    					}
    
    static StringBuffer y = new StringBuffer("A");
    						
     }

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Before worrying about threading issues, you will want to tackle using == with reference variables such as Strings. Much better is to use the equals or equalsIgnoreCase method or as you don't really care if one String variable refers to the same String object as another but rather whether they contain the same characters sequences.

  4. #4
    Andyj is offline Member
    Join Date
    Oct 2009
    Posts
    10
    Rep Power
    0

Similar Threads

  1. Problem in Multi threading.
    By Chetans in forum Advanced Java
    Replies: 3
    Last Post: 03-23-2010, 04:42 PM
  2. Getting problem in threading in JAVA
    By Chetans in forum Threads and Synchronization
    Replies: 3
    Last Post: 03-19-2010, 07:49 AM
  3. need some help with threading
    By dinosoep in forum New To Java
    Replies: 3
    Last Post: 12-03-2009, 05:31 PM
  4. Threading
    By jon80 in forum New To Java
    Replies: 1
    Last Post: 06-13-2009, 10:53 PM
  5. Problem in threading
    By saurabh in forum Threads and Synchronization
    Replies: 6
    Last Post: 12-01-2008, 08:16 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
  •