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

线程调用

时间:2019-11-21 17:03:55      阅读:94      评论:0      收藏:0      [点我收藏+]

标签:str   code   ble   print   new   run   system   oid   pre   

public class Test {
    private int j;

    public static void main(String args[]) {
        Test tt = new Test();
        Inc inc = tt.new Inc();
        Dec dec = tt.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();
            }
        }
    }
}

 

线程调用

标签:str   code   ble   print   new   run   system   oid   pre   

原文地址:https://www.cnblogs.com/lifan-xylm/p/11906411.html

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