zoukankan      html  css  js  c++  java
  • Java基础多线程之线程中止示例:

    class StopThreadDemo
    {
        public static void main(String[] args)
        {
            StopThread stopThread = new StopThread();
        
            Thread t1 = new Thread(stopThread,"t1");
            Thread t2 = new Thread(stopThread,"t2");
            
            t1.start();
            t2.start();
            
            int num=0;
            while(true)
            {
                if(num++ ==60)
                {            
                    //this.changeFlag();
                    t1.interrupt();
                    t2.interrupt();
                    break;
                }
                
                System.out.println(Thread.currentThread().getName()+ " runing.... "+ num);
            }
            
            System.out.println("end");
        }
    }

    class StopThread implements Runnable
    {
        private boolean flag = true;
        public synchronized void run()
        {        
            while(this.flag)
            {
                try
                {
                    wait();
                }
                catch(InterruptedException ex)
                {
                    System.out.println(Thread.currentThread().getName() + ": Exception");
                    this.changeFlag();
                }
                System.out.println(Thread.currentThread().getName() + ": runing...");
            }
        }
        
        public void changeFlag()
        {
            this.flag = false;
        }
    }
  • 相关阅读:
    必须克服的10种不良阅读习惯
    如何提升你的影响力
    爱情在线:拥抱变化的男女爱情!
    时间与生命
    女子最钟爱的五种男人
    社交常用表达:把人家的名字给忘了怎么办?
    最常用英语口头禅[推荐]
    中秋节的来历
    学会欣赏自己
    转帖]白领:4大行业通往高薪之路
  • 原文地址:https://www.cnblogs.com/cxmsky/p/2860991.html
Copyright © 2011-2022 走看看