• 死锁与进程通信


    死锁问题:不同的线程分别占用对方需要的同步资源不放弃,都在等待对方放弃自己需要的同步资源,就形成了线程的死锁

    TestDeadLock

    package com.aff.thread;
    //死锁的问题:处理线程同步时容易出现
    //不同的线程分别占用对方需要的同步资源不放弃,都在等待对方放弃自己需要的同步资源,就形成了线程的死锁
    //都握着一把锁,等着对方先释放,
    //写代码应避免死锁
    public class TestDeadLock { static StringBuffer sb1 = new StringBuffer(); static StringBuffer sb2 = new StringBuffer(); public static void main(String[] args) { new Thread() { public void run() { synchronized (sb1) { try { Thread.currentThread().sleep(10);//通过sleep放大 } catch (InterruptedException e) { e.printStackTrace(); } sb1.append("A"); synchronized (sb2) { sb2.append("B"); System.out.println(sb1); System.out.println(sb2); } } } }.start(); new Thread() { public void run() { synchronized (sb2) { try { Thread.currentThread().sleep(10); } catch (InterruptedException e) { e.printStackTrace(); } sb1.append("C"); synchronized (sb1) { sb2.append("D"); System.out.println(sb1); System.out.println(sb2); } } } }.start(); } }

    线程通信:

            wait():令当前线程挂起并放弃CPU、同步资源,使别的线程可访问并修改共享资源,而当前线程排队等候再次对资源的访问
          notify():唤醒正在排队等待同步资源的线程中优先级最高者结束等待
     notifyAll ():唤醒正在排队等待资源的所有线程结束等待.

    Java.lang.Object提供的这三个方法只有在synchronized方法或synchronized代码块中才能使用,

                               否则会报java.lang.IllegalMonitorStateException异常

     TestCommunication

    package com.aff.thread;
    
    //线程通信,如下的三个关键字的使用都得在 同步代码块或同步方法中
    //wait():一旦一个线程执行到wait(),就释放当前的锁
    //notify()/notifyAll():唤醒wait()的一个或所有线程
    //使用两个线程打印1-100,线程1,线程2 交替打印
    public class TestCommunication {
        public static void main(String[] args) {
            PrintNumer p = new PrintNumer();
            Thread t1 = new Thread(p);
            Thread t2 = new Thread(p);
            t1.start();
            t2.start();
        }
    }
    
    class PrintNumer implements Runnable {
        int num = 1;
    
        @Override
        public void run() {
            while (true) {
                synchronized (this) {
                    notify();// 进来后先唤醒上一个等待的(wait),然后结束自己再切换成wait(等待)
                    if (num <= 100) {
                        try {
                            Thread.currentThread().sleep(10);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        System.out.println(Thread.currentThread().getName() + ":" + num);
                        num++;
                    } else {
                        break;
                    }
                    try {
                        wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }
    All that work will definitely pay off
  • 相关阅读:
    BUPT复试专题—最长连续等差子数列(2014软院)
    BUPT复试专题—奇偶求和(2014软件)
    BUPT复试专题—网络传输(2014网研)
    Hopscotch(POJ 3050 DFS)
    Backward Digit Sums(POJ 3187)
    Smallest Difference(POJ 2718)
    Meteor Shower(POJ 3669)
    Red and Black(poj 1979 bfs)
    测试
    Equations(hdu 1496 二分查找+各种剪枝)
  • 原文地址:https://www.cnblogs.com/afangfang/p/12615983.html
Copyright © 2020-2023  润新知