package com.sync;
/*
*
* @author liaohongfei
*
* @Date 2015 2015-1-16 上午10:31:46
*
*/
public class Test {
Object object = new Object();
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
/**
* T1 和 test1,不加synchronized同步,线程会抢资源,两个线程同时运行,交错运行,执行不分先后。
*/
//new Test().test1();
/**
* T2 和 test2,加synchronized同步,当一个线程访问object的一个synchronized(this)同步代码块时,
* 其他线程对object中所有其它synchronized(this)同步代码块的访问将被阻塞,必须等待先访问的线程执行完。
* 也就是说,当一个线程访问object的一个synchronized(this)同步代码块时,它就获得了这个object的对象锁,
* 其它线程对该object对象所有同步代码部分的访问都被暂时阻塞。
*/
//new Test().test2();
/**
* T3_A/T3B 和 test3,当一个线程访问object的一个synchronized(this)同步代码块时,
* 另一个线程仍然可以访问该object中的非synchronized(this)同步代码块。
*/
new Test().test3();
/**
* 解决多线程同步的问题必须得获得对象锁,而且这个对象锁还得是同一把锁,这些线程才会排队运行;否则还是会并发运行。
* 普通对象的索和静态方法的锁有所不同,参考后面注释。
*/
}
void test1() {
T1 t1 = new Test().new T1();
Thread a = new Thread(t1, "A");
Thread b = new Thread(t1, "B");
a.start();
b.start();
}
class T1 implements Runnable {
@Override
public void run() {
// TODO Auto-generated method stub
for (int i = 0; i < 5; i++) {
System.out.println(Thread.currentThread().getName() + " synchronized loop " + i);
}
}
}
void test2() {
T2 t2 = new Test().new T2();
Thread a = new Thread(t2, "A");
Thread b = new Thread(t2, "B");
a.start();
b.start();
}
class T2 implements Runnable {
@Override
public void run() {
// TODO Auto-generated method stub
synchronized (object) {
for (int i = 0; i < 5; i++) {
System.out.println(Thread.currentThread().getName() + " synchronized loop " + i);
}
}
}
}
void test3() {
T3_B t3 = new Test().new T3_B();
Thread a = new Thread(t3, "A");
Thread b = new Thread(t3, "B");
a.start();
b.start();
}
class T3_A {
public void t3_a() {
synchronized (this) {
int i = 5;
while(i-- > 0) {
System.out.println(Thread.currentThread().getName() + " loop " + i);
try {
Thread.sleep(500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
public void t3_b() {
int i = 5;
while(i-- > 0) {
System.out.println(Thread.currentThread().getName() + " loop " + i);
try {
Thread.sleep(500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
class T3_B implements Runnable {
@Override
public void run() {
// TODO Auto-generated method stub
synchronized (object) {
for (int i = 0; i < 5; i++) {
System.out.println(Thread.currentThread().getName() + " synchronized loop " + i);
}
}
for (int i = 0; i < 5; i++) {
System.out.println(Thread.currentThread().getName() + " not synchronized loop " + i);
}
}
}
/**
* public class MyDemo {
private static String a="test";
//等同于方法print2
public synchronized void print1(String b){ //给方法print1加上对象锁,这个对象锁就是MyDemo实例
System.out.println(b+a);
}
public void print2(String b){
synchronized (this) { //this就是当前MyDemo实例对象的锁
System.out.println(b+a);
}
}
//等同于方法print4
public synchronized static void print3(String b){//静态方法,这个锁是MyDemo.class类
System.out.println(b+a);
}
public static void print4(String b){
synchronized (StaticSynDemo.class) { //取得MyDemo.class类的锁
System.out.println(b+a);
}
}
}
*/
}