码迷,mamicode.com
首页 > 编程语言 > 详细

转--秒杀多线程第四篇 一个经典的多线程同步问题(续)

时间:2014-11-04 17:01:23      阅读:196      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   ar   java   sp   div   art   问题   

demo in java:

package com.multithread.main.sampleExample;

import com.multithread.main.ExampleInterface;

public class SampleExample extends ExampleInterface{

    final int MAX_THREAD_NUM = 10;
    int g_num = 0;
    @Override
    public void startDemo() {
        int i=0;
        while(i<MAX_THREAD_NUM)
        {
            Thread t = new SampleMultiThread(i);
            t.start();
            i++;
        }
    }
    
    
    class SampleMultiThread extends Thread{

        int index = -1;
        public SampleMultiThread(int index)
        {
            this.index = index;            
        }
        
        @Override
        public void run() {
            // TODO Auto-generated method stub
            g_num++;
            System.out.println("g_num is:"+g_num +"\t"+"index is:"+this.index);
        }
        
        
        
    }

}

result:

g_num is:2    index is:0
g_num is:8    index is:6
g_num is:7    index is:9
g_num is:6    index is:7
g_num is:5    index is:5
g_num is:4    index is:3
g_num is:3    index is:2
g_num is:2    index is:1
g_num is:10    index is:8
g_num is:10    index is:4

转--秒杀多线程第四篇 一个经典的多线程同步问题(续)

标签:style   blog   color   ar   java   sp   div   art   问题   

原文地址:http://www.cnblogs.com/deman/p/4073757.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!