Results 1 to 12 of 12
  1. #1
    ibtehal_n is offline Member
    Join Date
    Sep 2011
    Posts
    7
    Rep Power
    0

    Default type of parameters in multithreading system

    Hello,
    I would like to ask about the type of parameters in multithreads what is the correct way?
    Define the parameters as private for each thread and calculate for example the average for each thread
    or define synchronized function to calculate the average for each thread? I wrote the following example code:
    Java Code:
    import java.net.*; 
    import javax.swing.SwingUtilities;
    import javax.swing.Timer;
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Toolkit;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.io.BufferedInputStream;
    import java.util.Date;
    import java.util.Random;
    import java.lang.Object;
    import java.lang.*;
    import java.io.Serializable;
    import java.util.*;   
    import java.util.TimerTask;
    import java.util.Vector;
    ////////////////////++++++++++++++++++++++Direct client++++++++++++++++++++=
    class Directclient extends Thread {
    	////////Direct client parameters///////////////////////
      
      private static int counter = 0;
      private int id = ++counter;//number of client
      private static int threadcount = 0;
      private long lastFlushTime1 = 0,lastFlushTime2 = 0,time=0,n=0;
      private  int total=0,h=0;
      
      
      public static int threadCount2() { 
        return threadcount; 
      }
      public Directclient( )throws IOException 
              {            
    			   			  
    			   start(); // Calls run()
             
              }//end constructor
        
        
     
      public void run() {
        
          for( int i = 1; i <=10; i++) {//10 requests for each client
           
            try{
            	  	  
            	  	
            	  	    if(i>=8)
            	  	    {    
    				       	       
    				       	       
    						       	lastFlushTime1 = System.currentTimeMillis(); 
    						       	sleep(1000);
    						        lastFlushTime2 = System.currentTimeMillis();
    						        test.time=(lastFlushTime2-lastFlushTime1);
    						        time=(lastFlushTime2-lastFlushTime1);
    						        test.n=test.n+time;
    						        
    						        test.total=test.total+1;
    						        test.t.Add(time);
            	  	    }
            	  	    else{
            	  	    	        lastFlushTime1 = System.currentTimeMillis(); 
    						       	sleep(2000);
    						        lastFlushTime2 = System.currentTimeMillis();
    						        test.time=(lastFlushTime2-lastFlushTime1);
    						        time=(lastFlushTime2-lastFlushTime1);
    						        test.n=test.n+test.time;
    						        test.total=test.total+1;
    						        test.t.Add(time);
            	  	    	
            	  	    	
            	  	    	
            	  	    }	 
            	  	    	       	 	
    				            
    				       	
    				                  
            	       
            }catch(Exception e) { System.err.println("IO Exception main try");}
         
          }   
                                         
          
          test.Browse.println("   "+ (double) ((double) test.n/(double) test.total)); 
           test.Browse1.println("   "+ test.t.Ave());  
           	test.Browse2.println("   "+ test.n);
           	test.Browse3.println("   "+ test.total);    	                                                              
                                                                        
          lastFlushTime1 = 0;lastFlushTime2 = 0;time=0;n=0; total=0;    
         
      }//end run
    }//end direct client thread
    
    
    ////////////////////++++++++++++++++++Portal Buy thread++++++++====
    
    
    public class test
    {
      	static final int MAX_THREADS=10;
      	public static PrintStream Browse = null;
      	public static PrintStream Browse1 = null;
      	public static PrintStream Browse2 = null;
      	public static PrintStream Browse3 = null;
    	public static int  total=0,m=0,h=0,x=0;
    	public static long n=0,time=0;
    	public static test1 t=new test1();
    	
    	public static void main(String[] args) throws IOException, InterruptedException {
       
        
        	//////////////////////////Open result files//////////////////
                    try {
                         
                         Browse = new PrintStream(new FileOutputStream("browse.xls"));
                         Browse1 = new PrintStream(new FileOutputStream("browse_1.xls"));
                          Browse2= new PrintStream(new FileOutputStream("n.xls"));
                           Browse3 = new PrintStream(new FileOutputStream("total.xls"));
                         }//end try
                     catch (IOException e){System.out.println("OOps");}	
        /////////////////////////////////////////////////////////////  
         
               while (x < MAX_THREADS) {
             
                  
          	
    	   try{
    	    	
    	    	
    	        Thread t3=new Directclient();
    	        
    	   }catch (IOException e){System.out.println("\nError....! " +e);} 
    	   
                    
            x++;
        } //end while
       
      
      }//end main
    }//end class ///:/
    I tried to use the two ways but I do not sure which one is the correct??

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

    Default Re: type of parameters in multithreading system

    What is the objective of your program?
    It appears that any of the threads can access the static variables in the test class at any time without consideration of what the other threads are doing with those variables.


    Your code does NOT follow the naming conventions. Uppercase first letter for class: Test vs test
    and lowercase for methods: add vs Add
    Last edited by Norm; 09-26-2011 at 03:59 PM.

  3. #3
    ibtehal_n is offline Member
    Join Date
    Sep 2011
    Posts
    7
    Rep Power
    0

    Default Re: type of parameters in multithreading system

    I have use another class to try synchronized function to get the average, by the way this example of multithreads programe and I want to calculate the average for each thread the code of class test1 is:
    Java Code:
    import java.io.Serializable; 
    
    public class test1 implements Serializable {
        
       
       private long time1=0;
       private int total=0;
       
        
       public test1()
       { 
       	  
       }
    /******************************************************************************/   
     public synchronized void Add(long e) 
       { 
          time1=time1+e; 
          total=total+1;	
       
       }
       public synchronized double Ave() 
       { 
       return (double) time1/total;
       }
    }
    so my question is what is the correct way to get the average for each thread without any interfering?

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

    Default Re: type of parameters in multithreading system

    Your code does NOT follow the naming conventions. Uppercase first letter for class: Test vs test & Test1 vs test1
    and lowercase for methods: add vs Add & ave vs Ave

    Not following the coding conventions makes it hard to see if you are using a static variable in a class or in an instance. test1.Add looks like test1 is an instance and Add is ???

  5. #5
    ibtehal_n is offline Member
    Join Date
    Sep 2011
    Posts
    7
    Rep Power
    0

    Default Re: type of parameters in multithreading system

    I have copied and pasted this code from Java programe without any syntax error , my question is related to logic part I have used two different way to clculate the average for each thread:
    1- define the variables as private for each thread and calculate the average inside the thread? or
    2- define the variables as public static in the main and use them for each thread then I reset these variables to avoid the interfering ?or
    3- define static class that include synchronized function to calculate the average for each thread?
    Hope that clear now?
    Thanks

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

    Default Re: type of parameters in multithreading system

    my question is related to logic
    Not following the conventions makes it hard to read and understand your code.
    Making the variables separate for each thread will prevent collisions.
    Using shared static variables means you have to worry about collisions.

    Do you get the same results when you run the code each of the two ways?

  7. #7
    ibtehal_n is offline Member
    Join Date
    Sep 2011
    Posts
    7
    Rep Power
    0

    Default Re: type of parameters in multithreading system

    No I got different results: same result for all threads and different results for each thread

  8. #8
    ibtehal_n is offline Member
    Join Date
    Sep 2011
    Posts
    7
    Rep Power
    0

    Default Re: type of parameters in multithreading system

    I think that when I define the private variables for each thread is correct but I am not sure that I have to calculate the average inside the thread or use class test1 to calculate the average using Ave() sunchronized function

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

    Default Re: type of parameters in multithreading system

    The running times could be different because of the OS doing things while your test are running.

  10. #10
    ibtehal_n is offline Member
    Join Date
    Sep 2011
    Posts
    7
    Rep Power
    0

    Default Re: type of parameters in multithreading system

    So, calculate the average inside each thread is the correct way?
    but what about synchronized methods in multithreads system? because this small part of my code I have to increase the number of thread to 1000 threads

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

    Default Re: type of parameters in multithreading system

    Why so many threads? They will only get in each others way and slow done the task.

  12. #12
    ibtehal_n is offline Member
    Join Date
    Sep 2011
    Posts
    7
    Rep Power
    0

    Default Re: type of parameters in multithreading system

    Actually this part of my client code that send many requests to server code and I just want to check the the calculations of averages are correct to complete my simulation.

Similar Threads

  1. Replies: 4
    Last Post: 08-01-2011, 10:29 AM
  2. multithreading
    By praveenbhushan1989 in forum Threads and Synchronization
    Replies: 15
    Last Post: 03-01-2011, 01:17 AM
  3. Replies: 2
    Last Post: 02-17-2011, 05:15 AM
  4. Replies: 5
    Last Post: 11-21-2010, 02:08 PM
  5. multithreading
    By shilpa.krishna in forum New To Java
    Replies: 2
    Last Post: 06-27-2008, 04:18 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
  •