A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 周飞飞 中级黑马   /  2015-6-22 14:00  /  265 人查看  /  3 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

class StopThread implements Runnable
{
        private boolean flag =true;
        public  void run()
        {
                while(flag)
                {
                       
                        System.out.println(Thread.currentThread().getName()+"....run");
                }
        }
        public void changeFlag()
        {
                flag = false;
        }
}




class  StopThreadDemo
{
        public static void main(String[] args)
        {
                StopThread st = new StopThread();
               
                Thread t1 = new Thread(st);
                Thread t2 = new Thread(st);


                t1.setDaemon(true);
                t2.setDaemon(true);
                t1.start();
                t2.start();

                int num = 0;

                while(true)
                {
                        if(num++ == 60)
                        {
                                //st.changeFlag();
                                //t1.interrupt();
                                //t2.interrupt();
                                break;
                        }
                        System.out.println(Thread.currentThread().getName()+"......."+num);
                }
                System.out.println("over");
        }
}

3 个回复

倒序浏览
不错,顶一个
回复 使用道具 举报
线程的停止方法
回复 使用道具 举报
赚个黑马币,走人
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马