zoukankan      html  css  js  c++  java
  • 圣思园java se培训总结(99-)(线程停止的方式)

    Thread类中有stop方法,但是不建议使用

    常用的停止线程的方法是

    package com.yuxi.lesson97;
    
    public class ControlThread
    {
    	static MyThread myThread = new MyThread();
    
    	public static void main(String[] args)
    	{
    		startThread();
    		// 某个需要的时刻去调用下面的结束方法
    		// stopThread();
    	}
    
    	private static void startThread()
    	{
    		new Thread(myThread).start();
    	}
    
    	private static void stopThread()
    	{
    		myThread.stopRun();
    	}
    }
    
    class MyThread implements Runnable
    {
    	private boolean flag = true;
    
    	@Override
    	public void run()
    	{
    		while (flag)
    		{
    			doSomeThing();
    		}
    	}
    
    	public void stopRun()
    	{
    		this.flag = false;
    	}
    
    	private void doSomeThing()
    	{
    
    	}
    
    }
    
  • 相关阅读:
    cookie操作
    css加载动画...
    三目运算符的运用
    遍历对象长度
    2年
    相亲

    股市周期
    功利心
    思考笔记
  • 原文地址:https://www.cnblogs.com/yuxishua/p/5103441.html
Copyright © 2011-2022 走看看