您的位置:首页 > 编程语言 > Java开发

java 的死锁的个人理解

2016-12-01 11:44 204 查看
多把锁的时候才会出现死锁,



import sun.jvm.hotspot.tools.JMap;

import java.lang.reflect.Method;

/**
* Created by zhangyinshan on 2016/11/30.
*/
public class SyncDemo {

// public static synchronized void staticSync(){
// System.out.println("static "+Thread.currentThread().getName());
// }
//
// public synchronized void notStaticSync(){
// System.out.println("not Static "+Thread.currentThread().getName());
// }
public static void main(String[] args){

// staticSync();
//
// SyncDemo syncDemo = new SyncDemo();
// syncDemo.notStaticSync();

for (int i = 0; i < 50; i++) {
test();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

public static void test(){

System.out.println("not Static "+Thread.currentThread().getName());

// Ticket ticket1 = new Ticket();
// Ticket ticket2 = new Ticket();
// Ticket ticket3 = new Ticket();
// Ticket ticket4 = new Ticket();
//
// Thread thread1 = new Thread(ticket1);
// Thread thread2 = new Thread(ticket2);
// Thread thread3 = new Thread(ticket3);
// Thread thread4 = new Thread(ticket4);
//
// thread1.start();
// thread2.start();
// thread3.start();
// thread4.start();

//如果是四个窗台一起买50张票,那么ticket只有一个对象

// Ticket ticket1 = new Ticket();
//
// Thread thread1 = new Thread(ticket1);
// Thread thread2 = new Thread(ticket1);
// Thread thread3 = new Thread(ticket1);
// Thread thread4 = new Thread(ticket1);
//
// thread1.start();
// thread2.start();
// thread3.start();
// thread4.start();

//上面的情况会出现例如第31张票在最后的时候才卖出,因为在例如thread2是31张票,当它想要执行输出的时候,cpu被其他的进程抢过去了
//
// TicketSync ticket1 = new TicketSync();
//
// Thread thread1 = new Thread(ticket1);
// Thread thread2 = new Thread(ticket1);
// Thread thread3 = new Thread(ticket1);
// Thread thread4 = new Thread(ticket1);
//
// thread1.start();
// thread2.start();
// thread3.start();
// thread4.start();

//函数的同步锁,默认的是对象本身,如果是static的话,那么锁就是该类的.class对象

//new Thread(new TicketSyncObject()).start();

//当有方法的锁是.class对象的,有的是类的变量的对象的时候就不是同步的了
//new Thread(new TicketSyncObjectNotStatic()).start();

//死锁,就是我要你的锁,你要我的锁导致的

new Thread(new TicketSyncObjectNotStaticDeadLock1()).start();
new Thread(new TicketSyncObjectNotStaticDeadLock2()).start();

//测试两个锁的顺序是一样的情况
//new Thread(new DeadLockShunXu1()).start();
//new Thread(new DeadLockShunXu2()).start();

}

}

class Ticket implements Runnable{

int num = 50;
@Override
public void run() {
while(num>0){
System.out.println(Thread.currentThread()+"sale ticket "+num);
num--;
}
}
}

class TicketSync implements Runnable{

int num = 50;
@Override
public synchronized void run() {
while(num>0){
System.out.println(Thread.currentThread()+"sale ticket "+num);
num--;
}
}
}

class TicketSyncObject implements Runnable{

@Override
public synchronized void run() {
new Thread(new Runnable(){

@Override
public void run() {
get();
}
}).start();

new Thread(new Runnable(){

@Override
public void run() {
put();
}
}).start();
}

public static synchronized int get(){
for (int i = 0; i < 100; i++) {

System.out.println(Thread.currentThread()+"get1 "+i);
System.out.println(Thread.currentThread()+"get2 "+i);
System.out.println(Thread.currentThread()+"get3 "+i);
}

return 1;
}

public static synchronized void put(){
//synchronized (TicketSyncObject.class) {
for (int i = 0; i < 100; i++) {
System.out.println(Thread.currentThread() + "put1 " + i);
System.out.println(Thread.currentThread() + "put2 " + i);
System.out.println(Thread.currentThread() + "put3 " + i);
}
//}
}
}

class TicketSyncObjectNotStatic implements Runnable{

@Override
public synchronized void run() {
new Thread(new Runnable(){

@Override
public void run() {
get();
}
}).start();

new Thread(new Runnable(){

@Override
public void run() {
put();
}
}).start();
}

public static synchronized void get(){
for (int i = 0; i < 100; i++) {

System.out.println(Thread.currentThread()+"get1 "+i);
System.out.println(Thread.currentThread()+"get2 "+i);
System.out.println(Thread.currentThread()+"get3 "+i);
}
}

public void put(){
synchronized (this) {
for (int i = 0; i < 100; i++) {
System.out.println(Thread.currentThread() + "put1 " + i);
System.out.println(Thread.currentThread() + "put2 " + i);
System.out.println(Thread.currentThread() + "put3 " + i);
}
}
}
}

class TicketSyncObjectNotStaticDeadLock1 implements Runnable{

@Override
public void run() {
synchronized (Integer.class){
System.out.println("DeadLock1 "+" get "+"Integer.class key "+" want Double.class key");
synchronized (Double.class){
System.out.println("DeadLock1 "+" get Double.class key");
}
}
}
}

class TicketSyncObjectNotStaticDeadLock2 implements Runnable{

@Override
public void run() {
synchronized (Double.class){

System.out.println("DeadLock2222 "+" get "+"Double.class key "+" want Integer.class key");

synchronized (Integer.class){
System.out.println("DeadLock2222 "+" get Double.class key");
}
}
}
}

class DeadLockShunXu1 implements Runnable{

@Override
public void run() {
synchronized (Integer.class){
System.out.println("DeadLock1 "+" get "+"Integer.class key "+" want Double.class key");
synchronized (Double.class){
System.out.println("DeadLock1 "+" get Double.class key");
}
}
}
}

class DeadLockShunXu2 implements Runnable{

@Override
public void run() {
synchronized (Integer.class){

System.out.println("DeadLock2222 "+" get "+"Integer.class key "+" want Double.class key");

synchronized (Double.class){
System.out.println("DeadLock2222 "+" get Double.class key");
}
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: