• Java多线程-一个简单的线程,实现挂起和恢复的功能



    主程式代码:

    public class MainApp {

        
    public static void main(String[] args) {
            
            
    try {
                MySprite dog 
    = new MySprite("狗狗");
                MySprite cat 
    = new MySprite("喵喵");
                MySprite pig 
    = new MySprite("猪猪");
                
                System.out.println(
    "--- start sprites");
                dog.start();
                cat.start();
                pig.start();
                Thread.sleep(
    500);
                System.out.println(
    "--- suspend dog");
                dog.suspend();
                System.out.println(
    "--- main thread do something");
                Thread.sleep(
    500);
                System.out.println(
    "--- resume dog");
                dog.resume();
                Thread.sleep(
    500);
                System.out.println(
    "--- end dog");
                dog.stop();
                System.out.println(
    "--- main thread do something");
                Thread.sleep(
    500);
                System.out.println(
    "--- end other sprites");
                cat.stop();
                pig.stop();
                Thread.sleep(
    100);
                System.out.println(
    "--- exit programe.");
            }
     catch (InterruptedException e) {
                e.printStackTrace();
            }

        }

    }


    线程实现

    public class MySprite implements Runnable {

        
    /*
         * 线程用变量
         
    */

        
    private boolean running = false;
        
    private boolean waiting = false;
        
    private Thread thread;
        
        
    /*
         * Business 变量
         
    */

        
    private String name;
        
        
    public MySprite(String name) {
            
    this.name = name;
            
    this.thread = new Thread(this);
        }

        
        
    /**
         * 启动线程
         
    */

        
    public void start() {
            running 
    = true;
            thread.start();
        }

        
        
    /**
         * 挂起线程
         
    */

        
    public void suspend() {
            
    if (waiting) // 是挂起状态则直接返回
                return;
            }

            
    synchronized (this{
                
    this.waiting = true;
            }

        }

        
        
    /**
         * 恢复线程
         
    */

        
    public void resume() {
            
    if (!waiting) // 没有挂起则直接返回
                return;
            }

            
    synchronized (this{
                
    this.waiting = false;
                
    this.notifyAll();
            }

        }

        
        
    /**
         * 停止线程
         
    */

        
    public void stop() {
            
    if (!running) // 没有运行则直接返回
                return;
            }

            
    synchronized (this{
                running 
    = false;
            }

        }

        
        
    public void run() {
            
    for(;;) {
                
    try {
                    
    // 线程挂起和退出处理
                    synchronized (this{
                        
    if (!running) {
                            
    break;
                        }

                        
    if (waiting) {
                            
    this.wait();
                        }

                    }


                    
    // 应该做的事情
                    cry();
                    
                    
    // 进入等待状态
                    Thread.sleep(50);
                }
     catch (InterruptedException e) {
                    e.printStackTrace();
                }

            }

        }

        
        
    private void cry() {
            System.out.println(name 
    + ":woo!");
        }

    }


  • 相关阅读:
    JS window对象 Navigator对象 Navigator 对象包含有关浏览器的信息,通常用于检测浏览器与操作系统的版本。
    JS window对象 返回下一个浏览的页面 forward()方法,加载 history 列表中的下一个 URL。
    JS window对象 取消计时器clearTimeout() setTimeout()和clearTimeout()一起使用,停止计时器。 语法: clearTimeout(id_of_setT
    JS window对象 History 对象 history对象记录了用户曾经浏览过的页面(URL),并可以实现浏览器前进与后退相似导航的功能。语法: window.history.[属性|方法]
    JS window对象 返回前一个浏览的页面 back()方法,加载 history 列表中的前一个 URL。 语法: window.history.back();
    JS window对象 计时器setTimeout() setTimeout()计时器,在载入后延迟指定时间后,去执行一次表达式,仅执行一次。 语法: setTimeout(代码,延迟时间);
    JS window对象取消计时器clearInterval() clearInterval() 方法可取消由 setInterval() 设置的交互时间。
    ELK问题处理
    nginx 日志变量含义
    nginx ssl 更换问题
  • 原文地址:https://www.cnblogs.com/hcfalan/p/1222960.html
Copyright © 2020-2023  润新知