ダネ第三課:マルチスレッド
6081 ワード
1 public class MyThread extends Thread {
2 public static int i=0;
3
4 public static synchronized void test(int x){
5 System.out.println(i+"::::");
6 i=x+i;
7 }
8
9 public void run(){
10 //the method is overwritten
11 // int i=0;
12 while(true){
13 // System.out.println(i+"::::");
14 // i++;
15 test(1);
16 if(i>1000)
17 break; //exit the loop
18 }
19 }
20
21
22 public static void main(String[] args) {
23 MyThread m= new MyThread();
24
25 StaticClassThread sct=new StaticClassThread();
26 Thread t = new Thread(sct);
27
28 m.start();
29 t.start();
30 }
31
32 public static class StaticClassThread implements Runnable{
33
34 @Override
35 public void run() {
36 // TODO Auto-generated method stub
37 // int i=0;
38 while(true){
39 // System.out.println("::::"+i);
40 // i--;
41 test(-1);
42 if(i<-1000)
43 break;
44 }
45 }
46
47 }
48 }