随笔 - 78  文章 - 25  trackbacks - 0
<2010年1月>
272829303112
3456789
10111213141516
17181920212223
24252627282930
31123456

常用链接

留言簿

随笔分类(75)

随笔档案(78)

相册

实用Links

我的Links

搜索

  •  

积分与排名

  • 积分 - 113243
  • 排名 - 514

最新评论

阅读排行榜

评论排行榜

设计4个线程,其中两个线程每次对j加1,另外两个线程对j每次减1。

/**
*Java multithread Test
*Date:2010-1-15
*Author:
*/

public class MultithreadTest{
    
private int j;

    
public static void main(String[] args){
        MultithreadTest mt
=new MultithreadTest();
        Inc inc
=mt.new Inc();
        Dec dec
=mt.new Dec();
        
for(int i=0;i<2;i++){
            Thread t
=new Thread(inc);
            t.start();
            t
=new Thread(dec);
            t.start();
        }
    }

    
private synchronized void inc(){
        j
++;
        System.out.println(Thread.currentThread().getName()
+"-inc:"+j);
    }
    
private synchronized void dec(){
        j
--;
        System.out.println(Thread.currentThread().getName()
+"-Dec:"+j);
    }


    
class Inc implements Runnable{
        
public  void run(){
            
for(int i=0;i<100;i++){
                inc();
            }
        }
    }

    
class Dec implements Runnable{
        
public  void run(){
            
for(int i=0;i<100;i++){
                dec();
            }
        }
    }
}

结果如下:
Thread-2-inc:77
Thread-2-inc:78
Thread-2-inc:79
Thread-2-inc:80
Thread-2-inc:81
Thread-2-inc:82
Thread-2-inc:83
Thread-2-inc:84
Thread-2-inc:85
Thread-2-inc:86
Thread-2-inc:87
Thread-2-inc:88
Thread-2-inc:89
Thread-2-inc:90
Thread-2-inc:91
Thread-2-inc:92
Thread-2-inc:93
Thread-2-inc:94
Thread-2-inc:95
Thread-3-Dec:94
Thread-3-Dec:93
Thread-3-Dec:92
Thread-3-Dec:91
Thread-3-Dec:90
Thread-3-Dec:89
Thread-3-Dec:88
Thread-3-Dec:87
Thread-3-Dec:86
Thread-3-Dec:85
Thread-3-Dec:84
Thread-3-Dec:83
Thread-3-Dec:82
Thread-3-Dec:81
Thread-3-Dec:80
Thread-3-Dec:79
Thread-3-Dec:78
Thread-3-Dec:77
Thread-3-Dec:76
Thread-3-Dec:75
Thread-3-Dec:74
Thread-3-Dec:73
Thread-3-Dec:72
Thread-3-Dec:71
Thread-3-Dec:70
Thread-3-Dec:69
Thread-3-Dec:68
Thread-3-Dec:67
Thread-3-Dec:66
Thread-3-Dec:65
Thread-3-Dec:64
Thread-3-Dec:63
Thread-3-Dec:62
Thread-3-Dec:61
Thread-3-Dec:60
Thread-3-Dec:59
Thread-3-Dec:58
Thread-3-Dec:57
Thread-3-Dec:56
Thread-3-Dec:55
Thread-3-Dec:54
Thread-3-Dec:53
Thread-3-Dec:52
Thread-3-Dec:51
Thread-3-Dec:50
Thread-3-Dec:49
Thread-3-Dec:48
Thread-3-Dec:47
Thread-3-Dec:46
Thread-3-Dec:45
Thread-3-Dec:44
Thread-3-Dec:43
Thread-3-Dec:42
Thread-3-Dec:41
Thread-3-Dec:40
Thread-3-Dec:39
Thread-3-Dec:38
Thread-3-Dec:37
Thread-3-Dec:36
Thread-3-Dec:35
Thread-3-Dec:34
Thread-3-Dec:33
Thread-3-Dec:32
Thread-3-Dec:31
Thread-3-Dec:30
Thread-3-Dec:29
Thread-3-Dec:28
Thread-3-Dec:27
Thread-3-Dec:26
Thread-3-Dec:25
Thread-3-Dec:24
Thread-3-Dec:23
Thread-3-Dec:22
Thread-3-Dec:21
Thread-3-Dec:20
Thread-3-Dec:19
Thread-3-Dec:18
Thread-3-Dec:17
Thread-3-Dec:16
Thread-3-Dec:15
Thread-3-Dec:14
Thread-3-Dec:13
Thread-3-Dec:12
Thread-3-Dec:11
Thread-3-Dec:10
Thread-3-Dec:9
Thread-3-Dec:8
Thread-3-Dec:7
Thread-3-Dec:6
Thread-3-Dec:5
Thread-3-Dec:4
Thread-3-Dec:3
Thread-3-Dec:2
Thread-3-Dec:1
Thread-3-Dec:0
Thread-3-Dec:-1
Thread-3-Dec:-2
Thread-3-Dec:-3
Thread-3-Dec:-4
Thread-3-Dec:-5
Thread-2-inc:-4
Thread-2-inc:-3
Thread-2-inc:-2
Thread-2-inc:-1
Thread-2-inc:0
Thread-2-inc:1
Thread-2-inc:2
Thread-2-inc:3
Thread-2-inc:4
Thread-2-inc:5
Thread-2-inc:6
Thread-1-Dec:5
Thread-1-Dec:4
Thread-1-Dec:3
Thread-1-Dec:2
Thread-1-Dec:1
Thread-1-Dec:0
Thread-1-Dec:-1
Thread-1-Dec:-2
Thread-0-inc:-1
Thread-0-inc:0
Thread-0-inc:1
Thread-0-inc:2
Thread-0-inc:3
Thread-0-inc:4
Thread-0-inc:5
Thread-0-inc:6
Thread-0-inc:7
Thread-0-inc:8
Thread-0-inc:9
Thread-0-inc:10
Thread-0-inc:11
Thread-0-inc:12
Thread-1-Dec:11
Thread-1-Dec:10
Thread-1-Dec:9
Thread-1-Dec:8
Thread-1-Dec:7
Thread-1-Dec:6
Thread-1-Dec:5
Thread-1-Dec:4
Thread-1-Dec:3
Thread-1-Dec:2
Thread-1-Dec:1
Thread-1-Dec:0
Thread-1-Dec:-1
Thread-1-Dec:-2
Thread-1-Dec:-3
Thread-1-Dec:-4
Thread-1-Dec:-5
Thread-1-Dec:-6
Thread-1-Dec:-7
Thread-1-Dec:-8
Thread-1-Dec:-9
Thread-1-Dec:-10
Thread-1-Dec:-11
Thread-1-Dec:-12
Thread-1-Dec:-13
Thread-1-Dec:-14
Thread-1-Dec:-15
Thread-1-Dec:-16
Thread-1-Dec:-17
Thread-1-Dec:-18
Thread-1-Dec:-19
Thread-1-Dec:-20
Thread-1-Dec:-21
Thread-2-inc:-20
Thread-2-inc:-19
Thread-2-inc:-18
Thread-2-inc:-17
Thread-2-inc:-16
Thread-2-inc:-15
Thread-2-inc:-14
Thread-2-inc:-13
Thread-2-inc:-12
Thread-2-inc:-11
Thread-2-inc:-10
Thread-1-Dec:-11
Thread-1-Dec:-12
Thread-1-Dec:-13
Thread-1-Dec:-14
Thread-1-Dec:-15
Thread-1-Dec:-16
Thread-1-Dec:-17
Thread-1-Dec:-18
Thread-1-Dec:-19
Thread-1-Dec:-20
Thread-1-Dec:-21
Thread-1-Dec:-22
Thread-1-Dec:-23
Thread-0-inc:-22
Thread-0-inc:-21
Thread-0-inc:-20
Thread-0-inc:-19
Thread-0-inc:-18
Thread-0-inc:-17
Thread-0-inc:-16
Thread-1-Dec:-17
Thread-1-Dec:-18
Thread-1-Dec:-19
Thread-1-Dec:-20
Thread-1-Dec:-21
Thread-1-Dec:-22
Thread-1-Dec:-23
Thread-1-Dec:-24
Thread-1-Dec:-25
Thread-1-Dec:-26
Thread-1-Dec:-27
Thread-1-Dec:-28
Thread-2-inc:-27
Thread-2-inc:-26
Thread-2-inc:-25
Thread-2-inc:-24
Thread-2-inc:-23
Thread-2-inc:-22
Thread-1-Dec:-23
Thread-1-Dec:-24
Thread-1-Dec:-25
Thread-1-Dec:-26
Thread-1-Dec:-27
Thread-1-Dec:-28
Thread-2-inc:-27
Thread-2-inc:-26
Thread-2-inc:-25
Thread-2-inc:-24
Thread-2-inc:-23
Thread-2-inc:-22
Thread-2-inc:-21
Thread-2-inc:-20
Thread-2-inc:-19
Thread-2-inc:-18
Thread-2-inc:-17
Thread-2-inc:-16
Thread-2-inc:-15
Thread-2-inc:-14
Thread-2-inc:-13
Thread-2-inc:-12
Thread-2-inc:-11
Thread-2-inc:-10
Thread-2-inc:-9
Thread-2-inc:-8
Thread-2-inc:-7
Thread-2-inc:-6
Thread-2-inc:-5
Thread-2-inc:-4
Thread-2-inc:-3
Thread-2-inc:-2
Thread-2-inc:-1
Thread-2-inc:0
Thread-2-inc:1
Thread-2-inc:2
Thread-2-inc:3
Thread-2-inc:4
Thread-2-inc:5
Thread-2-inc:6
Thread-2-inc:7
Thread-2-inc:8
Thread-2-inc:9
Thread-2-inc:10
Thread-2-inc:11
Thread-2-inc:12
Thread-2-inc:13
Thread-2-inc:14
Thread-2-inc:15
Thread-1-Dec:14
Thread-1-Dec:13
Thread-1-Dec:12
Thread-1-Dec:11
Thread-1-Dec:10
Thread-1-Dec:9
Thread-1-Dec:8
Thread-1-Dec:7
Thread-1-Dec:6
Thread-1-Dec:5
Thread-1-Dec:4
Thread-1-Dec:3
Thread-1-Dec:2
Thread-1-Dec:1
Thread-1-Dec:0



posted on 2010-01-15 21:49 期待明天 阅读(358) 评论(0)  编辑  收藏 所属分类: Java

只有注册用户登录后才能发表评论。


网站导航:
博客园   IT新闻   Chat2DB   C++博客   博问