Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 2nd, 2012  |  syntax: Java  |  size: 1.01 KB  |  hits: 25  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. public class T2 implements Runnable {
  2.  
  3.         private static Integer summa;
  4.  
  5.         public static void main(String[] args) {
  6.  
  7.         summa = new Integer(0);
  8.  
  9.                 for (int i = 0; i < 1000; i++) {
  10.                         T2 task = new T2(summa);
  11.                         Thread thread = new Thread(task);
  12.                         thread.start();
  13.                 }
  14.         System.out.println(summa);
  15.         }
  16.  
  17.         public T2(Integer summa) {
  18.                 this.summa = summa;
  19.         }
  20.         public synchronized void run() {
  21.                 summa++;
  22.                 System.out.println(summa);
  23.         }
  24. }
  25.  
  26. /** Säikeen luonti väärässä paikassa*/
  27. public class Test1 implements Runnable {
  28.  public static void main(String[] args) {
  29.    Test1 task = new Test1();
  30.    Thread thread = new Thread(task);
  31.    thread.start();
  32.  }
  33.  public Test1() {
  34.  }
  35.  
  36.  public void run() {
  37.    System.out.println("test ");
  38.  }
  39. }
  40.  
  41. /** Kaksi starttia */
  42. public class Test2 implements Runnable {
  43.   public static void main(String[] args) {
  44.     new Test2();
  45.   }
  46.  
  47.   public Test2() {
  48.     Thread t = new Thread(this);
  49.     t.start();
  50.   }
  51.  
  52.   public void run() {
  53.     System.out.println("Test ");
  54.   }
  55. }