例子程序01
public class MyThread extends Thread {
private int count = 5;
//synchronized给当前对象加锁
@Override
public synchronized void run() {
count--;
System.out.println(this.currentThread().getName() + " count = " + count);
}
public static void main(String[] args) {
/**
* 分析:当多个线程访问myThread方法时,以排队的方式进行处理(这里排队是按照cpu分配的先后顺序而定的)
* 一个线程想要执行synchronized关键字修饰的方法里的代码:
* 1.尝试获得锁
* 2.如果拿到锁,执行synchronized代码体内容,拿不到锁,这个线程就不断地尝试获得这把锁,直到拿到为止
* 而且多个线程同时取竞争这把锁(也就是会有锁竞争问题)
*/
MyThread myThread = new MyThread();
Thread t1 = new Thread(myThread,"t1");
Thread t2 = new Thread(myThread,"t2");
Thread t3 = new Thread(myThread,"t3");
Thread t4 = new Thread(myThread,"t4");
Thread t5 = new Thread(myThread,"t5");
t1.start();
t2.start();
t3.start();
t4.start();
t5.start();
}
}
例子程序02
/**
*
* synchronized关键字 取得的锁都是对象锁,而不是把一段代码(方法)当作锁
* 所以代码中哪个线程先执行synchronized关键字,哪个线程就持有该方法所属对象的锁(Lock)
* 在静态方法上加上synchronized关键字,表示锁定Class类的对象,类一级别的锁,(独占.class类)
*
*/
public class MultiThread
{
/*static*/
private static int num = 0;
/*static*/
public static synchronized void printNum(String tag) {
try{
if(tag.equals("a")) {
num = 100;
System.out.println("tag a, set number over!");
Thread.sleep(1000);
} else {
num = 200;
System.out.println("tag b, set number over!");
}
System.out.println("tag " + tag + ", num = " + num);
} catch(Exception e) {
e.printStackTrace();
}
}
//注意观察 run方法输出的顺序
public static void main(String[] args) {
final MultiThread m1 = new MultiThread();
final MultiThread m2 = new MultiThread();
Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
m1.printNum("a");
}
});
Thread t2 = new Thread(new Runnable(){
@Override
public void run() {
m2.printNum("b");
}
});
t2.start();
t1.start();
}
}
例子程序03
/**
* 对象锁的同步和异步
*/
public class MyObject {
/** 同步方法 */
public synchronized void method1() {
try {
System.out.println(Thread.currentThread().getName());
Thread.sleep(4000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
/** 异步方法 */
public void method2() {
System.out.println(Thread.currentThread().getName());
}
public static void main(String[] args) {
//加锁的话,也是加了1把锁,因为只有1个对象。锁是对象锁
final MyObject mo = new MyObject();
/***
* 分析:
* t1线程先持有object对象的Lock锁,t2线程可以以异步的方式调用对象中的非synchronized修饰的方法
* t1线程先持有object对象的Lock锁,t2线程如果在这个时候调用 对象中的同步(synchronized)方法则需等待,也就是同步
*/
Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
mo.method1();
}
},"t1");
Thread t2 = new Thread(new Runnable() {
@Override
public void run() {
//method2不需要等待锁,直接就执行了;但是method2则需要等待t1释放锁,4秒后
mo.method1();
}
},"t2");
t1.start();
t2.start();
}
}
例子程序04
/**
* 业务整体需要使用synchronized,保持业务的原子性
*
*/
public class DirtyRead {
private String name = "bjsxt";
private String value = "123";
public synchronized void setValue(String name, String value) {
this.name = name;
try {
Thread.sleep(4000);
} catch (InterruptedException e) {
e.printStackTrace();
}
this.value = value;
System.out.println("setValue invoked, name =" + this.name + " , value =" + this.value);
}
public void getValue() {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("getValue invoked, name =" + this.name + " , value =" + this.value);
}
public static void main(String[] args) {
final DirtyRead dr = new DirtyRead();
Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
dr.setValue("zhangsan", "456");
}
});
Thread t2 = new Thread(new Runnable() {
@Override
public void run() {
dr.getValue();
}
});
t1.start();
t2.start();
}
}