fork download
  1. package thread;
  2.  
  3. import java.util.ArrayList;
  4.  
  5. public class add extends Thread {
  6.  
  7. public static Object lock = new Object();
  8. private static ArrayList<Double> tmp = new ArrayList<Double>();
  9. private static int Thread_Num = 4;
  10. private static int count = -1;
  11.  
  12. private static double allsum = 0;
  13. private static double limit = 10000000;
  14.  
  15. public void run() { // override Thread's run()
  16.  
  17. int threadid;
  18. synchronized(lock) {
  19. threadid = ++count;
  20. }
  21. double sum = 0;
  22.  
  23. for(int i=threadid;i<limit;i=i+Thread_Num) {
  24. sum += tmp.get(i);
  25. }
  26.  
  27. synchronized(lock) {
  28. allsum += sum;
  29. }
  30.  
  31. System.out.println("Thread" + threadid + " finish!");
  32. }
  33.  
  34. public static void main(String[] argv) {
  35.  
  36. double testsum = 0;
  37. for(int i=0;i<limit;i++) {
  38. tmp.add((double) 1);
  39. testsum = testsum + 1;
  40. }
  41. double startTime = System.currentTimeMillis();
  42.  
  43. Thread t[] = new Thread [Thread_Num];
  44. for(int i=0;i<Thread_Num;i++) {
  45. t[i] = new add();
  46. t[i].start();
  47. }
  48.  
  49.  
  50. try {
  51. for(int i=0;i<Thread_Num;i++) {
  52. t[i].join();
  53. }
  54. } catch (InterruptedException e) {}
  55.  
  56. System.out.println("right ans");
  57. System.out.println(testsum);
  58.  
  59. System.out.println("thread ans");
  60. System.out.println(allsum);
  61.  
  62. double endTime = System.currentTimeMillis();
  63. System.out.println("Time : " + (endTime-startTime));
  64.  
  65.  
  66.  
  67. }
  68.  
  69.  
  70. }
  71.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
Main.java:5: class add is public, should be declared in a file named add.java
public class add extends Thread {
       ^
1 error
stdout
Standard output is empty