zoukankan      html  css  js  c++  java
  • 多线程

    public class 多线程 {
        static boolean flag = true;
        static class t1 implements Runnable{
            @Override
            public void run() {
                while (true){
                   Systrem.out.println("11");
                }
    
            }
        }
        public static void main(String[] args) throws IOException {
            new Thread(new t1()).start();
        }
    }        
    public class 多线程 {
        public static void main(String[] args) throws IOException {
            new Thread(() -> {
                ...
            }).start();
        }
    }

    synchronized的理解

    1  控制变量时使用锁代码块
    2  要注意锁代码块是放在循环里面还是外面
    3  sleep不会释放锁 wait会释放资源
    4 我感觉锁对象是一个字符串比较好使 , 使用this 比较常

     死锁

    public class 死锁 {
        public static void main(String[] args) {
            new Thread(() -> {
                synchronized ("aaa"){
                    System.out.println("aaa等待bbb锁");
                    //为了防止第二个线程还未启动便将bbb锁执行完
                    for (int i = 0; i < 1000000; i++) {
                        int k=0;k++;
                    }
                    synchronized ("bbb"){
                        System.out.println("aaa执行bbb");
                    }
                }
            }).start();
            new Thread(() -> {
                synchronized ("bbb"){
                    System.out.println("bbb等待aaa锁");
                    synchronized ("aaa"){
    
                    }
                }
            }).start();
        }
    }

     线程握手

    //线程握手的synchronized实现
    
    //线程握手的write()和notify()实现
    我凝视这恒星,等待这那场风暴,我已经准备好了
  • 相关阅读:
    NLB网路负载均衡管理器详解
    Nginx配置详解
    Nginx代理功能与负载均衡详解
    .Net使用RabbitMQ详解
    说说面向服务的体系架构SOA
    .Net中的RealProxy实现AOP
    搭建自己的Nuget服务器
    VMware虚拟网络连接模式详解(NAT,Bridged,Host-only)
    JsonUtils
    Linux三剑客
  • 原文地址:https://www.cnblogs.com/cheng5350/p/11947598.html
Copyright © 2011-2022 走看看