线程基础
线程安全:
当多个线程访问某一个类(对象或方法)时,这个对象始终都能表现出正确的行为,那么这个类(对象或方法)就是线程安全的
synchronized:
可以在任意对象及方法上加锁,而加锁的这段代码称为"互斥区"或"临界区"
例子:
public class MyThread extends Thread{ private int count = 5 ; //synchronized加锁 public void run(){ count--; System.out.println(this.currentThread().getName() + " count = "+ count); } public static void main(String[] args) { 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(); } } 输出结果:t2 count = 3
t1 count = 3
t4 count = 2
t3 count = 1
t5 count = 0
count值并没有按照顺序去减
下面 将run方法加上synchronized
public class MyThread extends Thread{ private int count = 5 ; //synchronized加锁 public synchronized void run(){ count--; System.out.println(this.currentThread().getName() + " count = "+ count); } public static void main(String[] args) { 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(); } } 输出结果:t1 count = 4 t3 count = 3 t4 count = 2 t2 count = 1 t5 count = 0
结论分析:
当多个线程访问myThread的run方法时,以排队的方式进行处理(这里排对是按照CPU分配的先后顺序而定的),一个线程想要执行synchronized修饰的方法里的代码:
1 尝试获得锁
2 如果拿到锁,执行synchronized代码体内容;拿不到锁,这个线程就会不断的尝试获得这把锁,直到拿到为止,
而且是多个线程同时去竞争这把锁。(也就是会有锁竞争的问题)
对象锁和类级别锁
关键字synchronized取得的锁都是对象锁,而不是把一段代码(方法)当做锁,所以代码中哪个线程先执行synchronized关键字的方法,哪个线程就持有该方法所属对象的锁(Lock)
在静态方法上加synchronized关键字,表示锁定.class类,类一级别的锁(独占.class类)。
列子
public class MultiThread { private int num = 0; /** static */ public synchronized void printNum(String tag){ try { if(tag.equals("a")){ num = 100; System.out.println("tag a, set num over!"); Thread.sleep(1000); } else { num = 200; System.out.println("tag b, set num over!"); } System.out.println("tag " + tag + ", num = " + num); } catch (InterruptedException 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"); } }); t1.start(); t2.start(); } 结果 tag a, set num over! tag b, set num over! tag b, num = 200 tag a, num = 100
结果分析: 线程m1 m2都执行了run方法,如果m1线程调到run方法时,获得了对象锁,m2等待m1执行完才执行,结果应该是 :
tag a, set num over! tag a, num = 100 tag b, set num over! tag b, num = 200
其实不然,m1 m2 是2个对象,有2把锁,没有冲突,所以都可以执行,可以使用类级别锁,输出结果会按照顺序
public class MultiThread { 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 num over!"); Thread.sleep(1000); } else { num = 200; System.out.println("tag b, set num over!"); } System.out.println("tag " + tag + ", num = " + num); } catch (InterruptedException 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"); } }); t1.start(); t2.start(); } 结果: tag a, set num over! tag a, num = 100 tag b, set num over! tag b, num = 200
结果分析:在静态方法上加synchronized关键字,表示锁定.class类,类一级别的锁(独占.class类),无论创建几个对象,调用printNum()方法,获取的都是类级别锁
结论:
关键字synchronized取得的锁都是对象锁,而不是把一段代码(方法)当做锁,所以代码中哪个线程先执行synchronized关键字的方法,哪个线程就持有该方法所属对象的锁(Lock),
在静态方法上加synchronized关键字,表示锁定.class类,类一级别的锁(独占.class类)。
对象锁的同步和异步
同步: synchronized
同步的概念就是共享,我们要牢牢记住共享这2个字,如果不是“共享”资源,就没必要进行同步
异步:
异步的概念就是独立,相互之间不受任何制约,就好像学习http的时候,在页面发起ajax请求,还可以继续浏览和操作页面的内容,二者之间没有任何关系
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()); } }
区别:
method1异步方法多个线程需要排队的方法,而method2同步方法多个线程可以并发的访问
例子
public class MyObject { public synchronized void method1(){ try { System.out.println(Thread.currentThread().getName()); Thread.sleep(4000); } catch (InterruptedException e) { e.printStackTrace(); } } /** synchronized */ public void method2(){ System.out.println(Thread.currentThread().getName()); } public static void main(String[] args) { final MyObject mo = new MyObject(); Thread t1 = new Thread(new Runnable() { @Override public void run() { mo.method1(); } },"t1"); Thread t2 = new Thread(new Runnable() { @Override public void run() { mo.method2(); } },"t2"); t1.start(); t2.start(); } } 结果: 同时输出 t1 t2,没有等待4秒输出t2
分析:
t1线程先持有object对象的Lock锁,t2线程可以以异步的方式调用对象中的非synchronized修饰的方法
t1线程先持有object对象的Lock锁,t2线程如果在这个时候调用对象中的同步(synchronized)方法则需等待,也就是同步
上一篇: java.net.UnknownHostException
下一篇: java线程的生命周期