zoukankan      html  css  js  c++  java
  • 多线程顺序的控制(wait,notity,sleep)

    public class abc extends Thread{
        private Object prev=null;
        private Object self=null;
        private String msg=null;
        public abc(Object prev,Object self,String msg){
            this.prev=prev;
            this.self=self;
            this.msg=msg;
        }
        public void run(){
            int count=0;
            while(count<1000){
                synchronized(prev){
                    synchronized(self){
                        System.out.print(msg);
                        if(msg.contains("C"))
                            System.out.println();
                        count++;
                        self.notify();
                    }
                    if(count==1000)
                    break;
                    try{
                        prev.wait();}
                    catch(Exception e)
                        {e.getMessage();}
                }
                
            }
            
        }
        public static void main(String[] args){
            Object a=new Object();
            Object b=new Object();
            Object c=new Object();
            abc abc1=new abc(c,a,"A");
            abc abc2=new abc(a,b,"B");
            abc abc3=new abc(b,c,"C");
            try{
            abc1.start();Thread.sleep(100);}catch(Exception e){e.getMessage();}
            try{
            abc2.start();Thread.sleep(100);}catch(Exception e){e.getMessage();}
            abc3.start();
        }
    }

  • 相关阅读:
    ubuntu命令
    mac获取root权限
    centos7安装解压缩工具 ncompress
    ubuntu17.04 配置go环境变量
    vue.js 拦截器
    ubuntu 安装jdk
    ubuntu安装deb文件
    初识 阿里云 SSL 证书申请
    java之XML
    LanProxy 内网映射穿透
  • 原文地址:https://www.cnblogs.com/heysong/p/3733477.html
Copyright © 2011-2022 走看看