您好,登錄后才能下訂單哦!
package com.skcc.mthread;
public class MuiltThread {
private static int num = 0;
public MuiltThread() {
// TODO Auto-generated constructor stub
}
/****
* synchronized void printNum(String tag) 代表 synchronized鎖定的MuiltThread實例對象鎖(m1,m2兩者間互不影響)
* static synchronized void printNum(String tag) 代表synchronized 競爭的是MuiltThread類鎖
*
* ****/
public static synchronized void printNum(String tag) {
try {
if(tag.equals("a")) {
num = 100;
System.out.println("tag a,num 100 set over.");
//Thread.sleep(1000);
}
else {
num=200;
System.out.println("tag b,num 200 set over.");
}
System.out.println("tag is " + tag + " num is "+ num);
} catch (Exception e) {
// TODO: handle exception
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
MuiltThread m1 = new MuiltThread();
MuiltThread m2 = new MuiltThread();
Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
//m1.printNum("a");
MuiltThread.printNum("a");
}
});
Thread t2 = new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
// m2.printNum("b");
MuiltThread.printNum("b");
}
});
t1.start();
t2.start();
}
}
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。