Results 1 to 12 of 12
- 09-26-2011, 03:13 PM #1
Member
- Join Date
- Sep 2011
- Posts
- 7
- Rep Power
- 0
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:
I tried to use the two ways but I do not sure which one is the correct??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 ///:/
- 09-26-2011, 03:57 PM #2
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 AddLast edited by Norm; 09-26-2011 at 03:59 PM.
- 09-26-2011, 05:07 PM #3
Member
- Join Date
- Sep 2011
- Posts
- 7
- Rep Power
- 0
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:
so my question is what is the correct way to get the average for each thread without any interfering?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; } }
- 09-26-2011, 05:10 PM #4
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 ???
- 09-26-2011, 05:24 PM #5
Member
- Join Date
- Sep 2011
- Posts
- 7
- Rep Power
- 0
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
- 09-26-2011, 05:33 PM #6
Re: type of parameters in multithreading system
Not following the conventions makes it hard to read and understand your code.my question is related to logic
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?
- 09-26-2011, 05:40 PM #7
Member
- Join Date
- Sep 2011
- Posts
- 7
- Rep Power
- 0
Re: type of parameters in multithreading system
No I got different results: same result for all threads and different results for each thread
- 09-26-2011, 05:43 PM #8
Member
- Join Date
- Sep 2011
- Posts
- 7
- Rep Power
- 0
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
- 09-26-2011, 05:46 PM #9
Re: type of parameters in multithreading system
The running times could be different because of the OS doing things while your test are running.
- 09-26-2011, 06:00 PM #10
Member
- Join Date
- Sep 2011
- Posts
- 7
- Rep Power
- 0
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
- 09-26-2011, 06:12 PM #11
Re: type of parameters in multithreading system
Why so many threads? They will only get in each others way and slow done the task.
- 09-26-2011, 06:18 PM #12
Member
- Join Date
- Sep 2011
- Posts
- 7
- Rep Power
- 0
Similar Threads
-
Trying to convert Java Date to type int to fit in mysql table with field type int(11)
By fortwnty420 in forum New To JavaReplies: 4Last Post: 08-01-2011, 10:29 AM -
multithreading
By praveenbhushan1989 in forum Threads and SynchronizationReplies: 15Last Post: 03-01-2011, 01:17 AM -
The Proper Class Design For Designing a Custom Language Type System
By abdulsami in forum Advanced JavaReplies: 2Last Post: 02-17-2011, 05:15 AM -
how to pass parameters from a method to another which accepts to parameters?possible?
By amrmb09 in forum Advanced JavaReplies: 5Last Post: 11-21-2010, 02:08 PM -
multithreading
By shilpa.krishna in forum New To JavaReplies: 2Last Post: 06-27-2008, 04:18 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks