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

    public class TestThread6 {
        public static void main(String[] args) throws InterruptedException {
            MyClass mo = new MyClass();
            Prooess p = new Prooess(mo);//同一个对象
    
            Thread t1 = new Thread(p);
            t1.setName("t1");
            Thread t2 = new Thread(p);
            t2.setName("t2");
    
            //启动线程
            t1.start();
            Thread.sleep(3000);
            t2.start();
        }
    }
    class Prooess implements Runnable{
        MyClass mo ;
        public Prooess(MyClass mo){
            this.mo = mo;
        }
        @Override
        public void run() {
            if(Thread.currentThread().getName().equals("t1")) {
                mo.m1();
            }
            if(Thread.currentThread().getName().equals("t2")){
                mo.m2();
             }
        }
    
    }
    
    
    class MyClass{
        public synchronized void m1(){
            //休眠
            try{
                Thread.sleep(10000);
            }catch (Exception e){
                e.printStackTrace();
            }
            System.out.println("m1");
        }
    //    public void m2(){//这里没有加锁  不需要等m1执行完直接输出
    ////        System.out.println("m2");
    ////    }
        public synchronized   void m2(){//同一把锁
            System.out.println("m2");
        }
    
    }
  • 相关阅读:
    翻转数组
    C语言之指针
    C语言之结构体
    C语言之函数
    数据结构之typedef
    数据结构之树
    数据结构之链表
    数据结构之队列
    数据结构之数组
    ssh远程连接控制 linux 口令、密钥连接
  • 原文地址:https://www.cnblogs.com/rzkwz/p/12417856.html
Copyright © 2011-2022 走看看