package com.chapter02.thread1;
public class HasSelfPrivateNum {
public void addI(String username) {
int num = 0;
try {
if (username.equals("a")) {
num = 100;
System.out.println("a set over!");
Thread.sleep(2000);
} else {
num = 200;
System.out.println("b set over!");
}
System.out.println(username + " num = " + num);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
package com.chapter02.thread1;
public class ThreadA extends Thread {
private HasSelfPrivateNum hasSelfPrivateNum;
public ThreadA(HasSelfPrivateNum hasSelfPrivateNum) {
super();
this.hasSelfPrivateNum = hasSelfPrivateNum;
}
@Override
public void run() {
super.run();
hasSelfPrivateNum.addI("a");
}
}
package com.chapter02.thread1;
public class ThreadB extends Thread {
private HasSelfPrivateNum hasSelfPrivateNum;
public ThreadB(HasSelfPrivateNum hasSelfPrivateNum) {
super();
this.hasSelfPrivateNum = hasSelfPrivateNum;
}
@Override
public void run() {
super.run();
hasSelfPrivateNum.addI("b");
}
}
package com.chapter02.thread1;
public class Run {
public static void main(String[] args) {
HasSelfPrivateNum hasSelfPrivateNum = new HasSelfPrivateNum();
ThreadA threadA = new ThreadA(hasSelfPrivateNum);
threadA.start();
ThreadB threadB = new ThreadB(hasSelfPrivateNum);
threadB.start();
}
}
a set over!
b set over!
b num = 200
a num = 100
方法中的变量不存在非线程安全问题,永远都是线程安全的。
package com.chapter02.thread1;
public class HasSelfPrivateNum {
//todo 注意此处 synchronized 和 变量
private int num = 0;
synchronized public void addI(String username) {
try {
if (username.equals("a")) {
num = 100;
System.out.println("a set over!");
Thread.sleep(2000);
} else {
num = 200;
System.out.println("b set over!");
}
System.out.println(username + " num = " + num);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
package com.chapter02.thread1;
public class ThreadA extends Thread {
private HasSelfPrivateNum hasSelfPrivateNum;
public ThreadA(HasSelfPrivateNum hasSelfPrivateNum) {
super();
this.hasSelfPrivateNum = hasSelfPrivateNum;
}
@Override
public void run() {
super.run();
hasSelfPrivateNum.addI("a");
}
}
package com.chapter02.thread1;
public class ThreadB extends Thread {
private HasSelfPrivateNum hasSelfPrivateNum;
public ThreadB(HasSelfPrivateNum hasSelfPrivateNum) {
super();
this.hasSelfPrivateNum = hasSelfPrivateNum;
}
@Override
public void run() {
super.run();
hasSelfPrivateNum.addI("b");
}
}
package com.chapter02.thread1;
public class Run {
public static void main(String[] args) {
HasSelfPrivateNum hasSelfPrivateNum = new HasSelfPrivateNum();
ThreadA threadA = new ThreadA(hasSelfPrivateNum);
threadA.start();
ThreadB threadB = new ThreadB(hasSelfPrivateNum);
threadB.start();
}
}
//如果不添加synchronized
a set over!
b set over!
b num = 200
a num = 200
//如果添加synchronized
a set over!
a num = 100
b set over!
b num = 200
在两个线程访问同一个对象中的同步方法时一定是线程安全的。
package com.chapter02.thread1;
public class Run {
public static void main(String[] args) {
HasSelfPrivateNum hasSelfPrivateNumA = new HasSelfPrivateNum();
ThreadA threadA = new ThreadA(hasSelfPrivateNumA);
threadA.start();
HasSelfPrivateNum hasSelfPrivateNumB = new HasSelfPrivateNum();
ThreadB threadB = new ThreadB(hasSelfPrivateNumB);
threadB.start();
}
}
a set over!
b set over!
b num = 200
a num = 100
上面的示例 创建了2个HasSelfPrivateNum类的对象,所以产生了2个锁。