t1.start();
t1.join();
t2.start();
复制代码
public class DeadLock {
public static String obj1 = "obj1";
public static String obj2 = "obj2";
public static void main(String[] args){
Thread a = new Thread(new Lock1());
Thread b = new Thread(new Lock2());
a.start();
b.start();
}
}
class Lock1 implements Runnable{
@Override
public void run(){
try{
System.out.println("Lock1 running");
while(true){
synchronized(DeadLock.obj1){
System.out.println("Lock1 lock obj1");
Thread.sleep(3000);//获取obj1后先等一下子,让Lock2有足够的时间锁住obj2
synchronized(DeadLock.obj2){
System.out.println("Lock1 lock obj2");
}
}
}
}catch(Exception e){
e.printStackTrace();
}
}
}
class Lock2 implements Runnable{
@Override
public void run(){
try{
System.out.println("Lock2 running");
while(true){
synchronized(DeadLock.obj2){
System.out.println("Lock2 lock obj2");
Thread.sleep(3000);
synchronized(DeadLock.obj1){
System.out.println("Lock2 lock obj1");
}
}
}
}catch(Exception e){
e.printStackTrace();
}
}
}
复制代码
没有设置Timeout参数的Object.wait()
没有设置Timeout参数的Thread.join()
LockSupport.park()
复制代码
Thread.sleep()
设置了Timeout参数的Object.wai()
设置了Timeout参数的Thread.join()
LockSupport.parkNanos()
LockSupport.parkUntil()
复制代码
private void test() {
final TestSynchronized test1 = new TestSynchronized();
final TestSynchronized test2 = new TestSynchronized();
Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
test1.method01("a");
//test1.method02("a");
}
});
Thread t2 = new Thread(new Runnable() {
@Override
public void run() {
test2.method01("b");
//test2.method02("a");
}
});
t1.start();
t2.start();
}
private static class TestSynchronized{
private int num1;
public synchronized void method01(String arg) {
try {
if("a".equals(arg)){
num1 = 100;
System.out.println("tag a set number over");
Thread.sleep(1000);
}else{
num1 = 200;
System.out.println("tag b set number over");
}
System.out.println("tag = "+ arg + ";num ="+ num1);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
private static int num2;
public static synchronized void method02(String arg) {
try {
if("a".equals(arg)){
num2 = 100;
System.out.println("tag a set number over");
Thread.sleep(1000);
}else{
num2 = 200;
System.out.println("tag b set number over");
}
System.out.println("tag = "+ arg + ";num ="+ num2);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
//调用method01方法打印日志【普通方法】
tag a set number over
tag b set number over
tag = b;num =200
tag = a;num =100
//调用method02方法打印日志【static静态方法】
tag a set number over
tag = a;num =100
tag b set number over
tag = b;num =200
复制代码
private void test3() {
Thread thread1 = new Thread(new Runnable() {
@Override
public void run() {
new VolatileExample().writer();
}
});
Thread thread2 = new Thread(new Runnable() {
@Override
public void run() {
new VolatileExample().reader();
}
});
thread1.start();
thread2.start();
}
public class VolatileExample {
private int a = 0;
private volatile boolean flag = false;
public void writer(){
a = 1; //1
LogUtils.e("测试volatile数据1--"+a);
flag = true; //2
LogUtils.e("测试volatile数据2--"+flag);
}
public void reader(){
LogUtils.e("测试volatile数据3--"+flag);
if(flag){ //3
int i = a; //4
LogUtils.e("测试volatile数据4--"+i);
}
}
}
复制代码
//第一种状况
2019-03-07 17:17:30.294 25764-25882/com.ycbjie.other E/TestFirstActivity: │ 测试volatile数据3--false
2019-03-07 17:17:30.294 25764-25881/com.ycbjie.other E/TestFirstActivity: │ 测试volatile数据1--1
2019-03-07 17:17:30.295 25764-25881/com.ycbjie.other E/TestFirstActivity: │ 测试volatile数据2--true
//第二种状况
2019-03-07 17:18:01.965 25764-25901/com.ycbjie.other E/TestFirstActivity: │ 测试volatile数据1--1
2019-03-07 17:18:01.965 25764-25902/com.ycbjie.other E/TestFirstActivity: │ 测试volatile数据3--false
2019-03-07 17:18:01.966 25764-25901/com.ycbjie.other E/TestFirstActivity: │ 测试volatile数据2--true
复制代码
private volatile int a = 0;
for (int x=0 ; x<=100 ; x++){
new Thread(new Runnable() {
@Override
public void run() {
a++;
Log.e("小杨逗比Thread-------------",""+a);
}
}).start();
}
复制代码
for (int x=0 ; x<=100 ; x++){
new Thread(new Runnable() {
@Override
public void run() {
AtomicInteger atomicInteger = new AtomicInteger(a++);
int i = atomicInteger.get();
Log.e("小杨逗比Thread-------------",""+i);
}
}).start();
}
复制代码