Results 1 to 2 of 2
  1. #1
    yifat60 is offline Member
    Join Date
    Nov 2011
    Posts
    1
    Rep Power
    0

    Default Multi-threading programing .

    HI.
    i am writting a very simple programe using muti-threading.
    need your advice where should i add the sychronayze if at all..
    Java Code:
    public class CoinFlipping implements Runnable {
    
    	
       
    	public void run() {
    	 int rundomNumber;
    	
    	 int counter=0;
    	
    	 for (int i = 0; i <= 1000; i++) {
    	      
    	      Random rand=new Random();
    		   rundomNumber=rand.nextInt(2);
    	
    		 if ( counter >=3 )
    		 {
    			 System.out.printf("%s got %d heads in a row.%n",
                         Thread.currentThread().getName(), 
                         counter);
    		 }
    		 
    		 if (rundomNumber == 1)
    			 counter ++;
    		 else 
    			 counter=0;
             
    	 }
    		
    	}
    	public  CoinFlipping(int num){
    	
    		ExecutorService taskList = 
    		      Executors.newFixedThreadPool(10);
    		
    		 for (int i = 0; i < num; i++) {
    			  taskList.execute(this);
    		
    	}
    	}
    then i call to the main programe with :
    new ConiFlipping(5)

    thanks!
    Last edited by Norm; 11-27-2011 at 04:34 PM. Reason: added code tags

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,562
    Rep Power
    25

    Default Re: Multi-threading programing .

    You would use synchronized where there is a possibility of more than one thread updating a variable at the same time.
    You want the threads to take turns and not try to update a value at the same time.

Similar Threads

  1. Multi-threading issue
    By StevenTNorris in forum Advanced Java
    Replies: 2
    Last Post: 10-19-2011, 09:08 PM
  2. multi-threading problem
    By imorio in forum New To Java
    Replies: 1
    Last Post: 04-25-2011, 06:52 PM
  3. Need Help! Multi-Threading question!
    By pinkette in forum New To Java
    Replies: 8
    Last Post: 01-13-2011, 07:08 PM
  4. Problem in Multi threading.
    By Chetans in forum Advanced Java
    Replies: 3
    Last Post: 03-23-2010, 04:42 PM
  5. question about Multi threading in Java
    By fred in forum Advanced Java
    Replies: 1
    Last Post: 07-24-2007, 01:55 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
  •