为什么会有线程同步的概念呢?为什么要同步?什么是线程同步?先看一段代码:

package com.maso.test;

public class ThreadTest2 implements Runnable{
private TestObj testObj = new TestObj(); public static void main(String[] args) {
ThreadTest2 tt = new ThreadTest2();
Thread t1 = new Thread(tt, "thread_1");
Thread t2 = new Thread(tt, "thread_2");
t1.start();
t2.start();
} @Override
public void run() { for(int j = 0; j < 10; j++){
int i = fix(1);
try {
Thread.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
} System.out.println(Thread.currentThread().getName() + " : i = " + i);
} } public int fix(int y){
return testObj.fix(y);
} public class TestObj{
int x = 10; public int fix(int y){
return x = x - y;
}
} }

输出结果后,就会发现变量x被两个线程同时操作,这样就很容易导致误操作。如何才能解决这个问题呢?用线程的同步技术,加上synchronized关键字

	public synchronized int fix(int y){
return testObj.fix(y);
}

加上同步后,就可以看到有序的从9输出到-10.

如果加到TestObj类的fix方法上能不能实现同步呢?

	public class TestObj{
int x = 10; public synchronized int fix(int y){
return x = x - y;
}
}

如果将synchronized加到方法上则等价于

	synchronized(this){

	}

可以判断出两个线程使用的TestObj类的同一个实例testOjb,所以后实现同步,但是输出的结果却不是理想的结果。这是因为当A线程执行完x = x - y后还没有输出则B线程已经进入开始执行x = x - y.

所以像下面这样输出就不会有什么问题了:

	public class TestObj{
public TestObj(){
System.out.println("调用了构造函数");
} int x = 10; public synchronized int fix(int y){
x = x - y;
System.out.println(Thread.currentThread().getName() + " : x = " + x);
return x;
}
}

如果将外部的fix方法修改如下:

	public int fix(int y){
ax++ ;
if(ax%2 == 0){
return testObj.fix(y, testObj.str1);
}else{
return testObj.fix(y, testObj.str2);
}
}
	public class TestObj{
String str1 = "a1";
String str2 = "a2"; public TestObj(){
System.out.println("调用了构造函数");
} int x = 10; public int fix(int y, String str){
synchronized (str) {
x = x - y;
System.out.println(Thread.currentThread().getName() + " : x = " + x);
}
return x;
}
}

此时synchronized中的str对象不是同一个对象,所以两个线程所持有的对象锁不是同一个,这样就不能实现同步。要实现线程之间的互斥就要使用同一个对象锁。

什么是死锁呢?举个例子就是比如你和同学租了个两室的房子,你拿着你房子的钥匙,你同学拿着他房子的钥匙,现在你在房子等你同学将他的钥匙给你然后你进他房子,你同学在他的房子等你将钥匙给他然后他进你的房子,这样就死锁了。

package com.maso.test;

public class ThreadDieSock implements Runnable {
private int flag = 1;
private Object obj1 = new Object(), obj2 = new Object(); public void run() {
System.out.println("flag=" + flag);
if (flag == 1) {
synchronized (obj1) {
System.out.println("我已经锁定obj1,休息0.5秒后锁定obj2去!");
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
synchronized (obj2) {
System.out.println("1");
}
}
}
if (flag == 0) {
synchronized (obj2) {
System.out.println("我已经锁定obj2,休息0.5秒后锁定obj1去!");
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
synchronized (obj1) {
System.out.println("0");
}
}
}
} public static void main(String[] args) {
ThreadDieSock run01 = new ThreadDieSock();
ThreadDieSock run02 = new ThreadDieSock();
run01.flag = 1;
run02.flag = 0;
Thread thread01 = new Thread(run01);
Thread thread02 = new Thread(run02);
System.out.println("线程开始喽!");
thread01.start();
thread02.start();
}
}

最新文章

  1. WPF 保存image控件里的图片
  2. [BI项目记]-配置Sharepoint2013支持文档版本管理笔记
  3. Mindmanager安装
  4. 介绍 .Net工具Code Snippet 与 Sql Server2008工具SSMS Tools Pack
  5. [算法] Manacher算法线性复杂度内求解最长回文子串
  6. define预处理以及宏定义
  7. ags模版与vs
  8. Gulp-livereload:实时刷新编码
  9. jsf小例子
  10. IE11仿真文档模式默认IE5 IE7的调整办法
  11. IDEA的破解安装以及汉化
  12. js根据出生年月日换算年龄
  13. Ajax常见面试题
  14. HDU 3565 Bi-peak Number(数位DP)题解
  15. 深入理解java中的底层阻塞原理及实现
  16. centos7破解安装fisheye和Crucible
  17. 用Eclipse在Weka中嵌入新算法
  18. Eclipse版本列表
  19. poj 3352 Road Construction(边双连通分量+缩点)
  20. Android四种布局方式

热门文章

  1. 编写一个程序,把指定目录下的所有的带.java文件都拷贝到另一个目录中,拷贝成功后,把后缀名是.java的改成.txt。
  2. 阿姆达尔定律(Amdahl's law)
  3. golang API 例子实现
  4. 洛谷 【P1252】马拉松接力赛
  5. 洛谷—— P1190 接水问题
  6. Hibernate之API初识及增删改查实现
  7. vue 星星评分组件
  8. Varnish 实战
  9. oracle函数大全-字符串处理函数
  10. (转)linux screen 命令详解