码迷,mamicode.com
首页 > 编程语言 > 详细

死锁,线程协作(同步,阻塞队列,Condition,管道流)

时间:2019-03-24 17:28:18      阅读:209      评论:0      收藏:0      [点我收藏+]

标签:管道流   ide   get   trace   over   nbsp   package   string   while   

 

技术图片

synchronized死锁
package com.thread.demo.deadlock;

public class DeadLock {
    private static Object lock1 = new Object();
    private static Object lock2 = new Object();

    public static void main(String[] args) {
        // 创建线程1
        new Thread(new Runnable() {

            @Override
            public void run() {
                while (true) {
                    synchronized (lock1) {
                        System.out.println(Thread.currentThread().getName() + "获取到lock1这把锁");
                        System.out.println(Thread.currentThread().getName() + "等待lock2锁..........");
                        synchronized (lock2) {
                            System.out.println(Thread.currentThread().getName() + "获取到lock2这把锁");
                        }
                    }
                }

            }
        }, "A线程").start();

        // 创建的线程2
        new Thread(new Runnable() {

            @Override
            public void run() {
                while (true) {
                    synchronized (lock2) {
                        System.out.println(Thread.currentThread().getName() + "获取到lock2这把锁");
                        System.out.println(Thread.currentThread().getName() + "等待lock1锁..........");
                        synchronized (lock1) {
                            System.out.println(Thread.currentThread().getName() + "获取到lock1这把锁");
                        }
                    }
                }

            }
        }, "B线程").start();
    }
}
ReentrantLock死锁
package com.thread.demo.deadlock;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class ReentrantDeadLock {
    private static Lock lock = new ReentrantLock();

    public static void main(String[] args) {
        new Thread(new MyRunnable(lock), "一线程").start();
        new Thread(new MyRunnable(lock), "二线程").start();
    }

}

class MyRunnable implements Runnable {

    private Lock lock;
    private static int count = 0;

    public MyRunnable(Lock lock) {
        this.lock = lock;
    }

    @Override
    public void run() {
        lock.lock();
        
        try {
        for (int i = 0; i < 100000000; i++) {
            count++;
            
            if (i == 100000) {
                throw new RuntimeException();
            }
        }
        System.out.println(Thread.currentThread().getName() + ":count=" + count);
        } catch(Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
        

    }

}

 

死锁,线程协作(同步,阻塞队列,Condition,管道流)

标签:管道流   ide   get   trace   over   nbsp   package   string   while   

原文地址:https://www.cnblogs.com/sunBinary/p/10589030.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!