4月16日,TestSecurity,每日20行。

来源:互联网 发布:浮云网淘宝小号专卖 编辑:程序博客网 时间:2024/06/14 04:24
class Resource implements Runnable {    volatile public int i;    volatile public Integer it;    public Resource(int _i){        i = _i;        it = new Integer(i);    }    public void run(){        while(true){            synchronized(it){                if(i > 0){                    try{                        Thread.sleep(200);                    }                    catch(Exception e){}                    i--;                    System.out.println(Thread.currentThread().getName() + " " + i);                }                else{                    System.out.println(Thread.currentThread().getName());                    break;                }            }        }    }}public class TestSecurity {    public static void main(String[] args) {        Resource m = new Resource(9);        Thread t1 = new Thread(m);        Thread t2 = new Thread(m);        t1.start();        t2.start();    }}

这里写图片描述

0 0
原创粉丝点击