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();
}
複製代碼