java线程同步
1.一个实例(线程同步会出现的问题)
package com.ru.thread;
public class synchronization implements Runnable {
timer timer=new timer();
@Override
public void run() {
// 调用timer的方法并获取当前线程的名字
timer.add(Thread.currentThread().getName());
}
public static void main(String[] args) {
// TODO Auto-generated method stub
synchronization syn=new synchronization();
Thread th1=new Thread(syn);
Thread th2=new Thread(syn);
//设置线程的名字
th1.setName(“th1″);
th2.setName(“th2″);
th1.start();
th2.start();
}
}
class timer{
public static int num=0;
public void add(String name){
num++;
try {
Thread.sleep(1000);
} catch (Exception e) {
// TODO: handle exception
}
System.out.println(name+”你是第”+num+”个”);
}
}
这个在运行时两个线程会因为被打断,而出现错误。所以线程同步问题需要解决
2.解决上面例子中的线程同步问题(synchronized(this){}给当前线程加锁)
(1)第一种写法
class timer{
public static int num=0;
public void add(String name){
//使用Synchronized给线程加锁
synchronized(this){
num++;
try {
Thread.sleep(1000);
} catch (Exception e) {
// TODO: handle exception
}
System.out.println(name+”你是第”+num+”个”);
}
}
}
(2)第二种写法
class timer{
public static int num=0;
public synchronized void add(String name){
//使用Synchronized给线程加锁
num++;
try {
Thread.sleep(1000);
} catch (Exception e) {
// TODO: handle exception
}
System.out.println(name+”你是第”+num+”个”);
}
}
3。死锁实例
package com.ru.thread;
public class DeadLock implements Runnable{
public int flag=0;
Object obj1=new Object();
Object obj2=new Object();
public static void main(String[] args) {
DeadLock dl1=new DeadLock();
DeadLock dl2=new DeadLock();
Thread t1=new Thread(dl1);
Thread t2=new Thread(dl2);
dl1.flag=0;
dl2.flag=1;
t1.start();
t2.start();
}
@Override
public void run() {
if (flag==0) {
synchronized (obj1) {
try {
System.out.println(“锁定obj1″);
Thread.sleep(500);
} catch (Exception e) {
e.printStackTrace();
}
synchronized(obj2) {
System.out.println(“t1–0″);
}
}
}
if (flag==1) {
synchronized (obj2) {
try {
System.out.println(“锁定obj2″);
Thread.sleep(500);
} catch (Exception e) {
e.printStackTrace();
}
synchronized(obj1) {
System.out.println(“t2–1″);
}
}
}
}
}
4.方法上锁后的运行结果
(1)
package com.ru.thread;
public class TT implements Runnable{
int i=1000;
public static void main(String[] args) {
// TODO Auto-generated method stub
TT tt=new TT();
Thread t=new Thread(tt);
t.start();
tt.m2();
}
@Override
public void run() {
// TODO Auto-generated method stub
m1();
}
public synchronized void m2(){
i=3000;
System.out.println(“运行m2″);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(“m2–i=”+i);
}
public synchronized void m1(){
i=2000;
System.out.println(“运行m1″);
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(“m1–i=”+i);
}
}
结果:
运行m2
m2–i=3000
运行m1
m1–i=2000
(2)
package com.ru.thread;
public class TT implements Runnable{
int i=1000;
public static void main(String[] args) {
// TODO Auto-generated method stub
TT tt=new TT();
Thread t=new Thread(tt);
t.start();
tt.m2();
}
@Override
public void run() {
// TODO Auto-generated method stub
m1();
}
public void m2(){
i=3000;
System.out.println(“运行m2″);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(“m2–i=”+i);
}
public synchronized void m1(){
i=2000;
System.out.println(“运行m1″);
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(“m1–i=”+i);
}
}
结果:
运行m2
运行m1
m2–i=2000
m1–i=2000