如何处理JFrame中多个Java线程的终止和暂停? [英] How to handle termination and suspension of multiple Java Threads in JFrame?

查看:263
本文介绍了如何处理JFrame中多个Java线程的终止和暂停?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个基于JFrame的Java GUI应用程序,该应用程序使用控制多个Java线程的Java类:

I have a Java GUI application based on JFrame, which uses a Java Class that controls multiple Java Threads:

我正在尝试控制从Control Class到的任何线程

I'm trying to control any thread from Control Class to

  1. 终止线程
  2. 暂停线程
  3. 取消暂停线程

到目前为止,Control Class可以创建一个线程并将其添加到ArrayList中以跟踪所有创建的线程:

So far, Control Class can create a thread and add it to ArrayList to keep track of all created threads:

public class Control 
{
ArrayList<myThread> threads;
int counter;

Control()
{
    // do construction work..
}

int createThreadAndRun(String keyword)
{
    // create an instance of myThread:
    counter++;

    myThread mythread       = new myThread( keyword, counter);
    mythread.runnable       = true;
    mythread.start();

    // add it to ArrayList:
    threads.add(mythread);

    return counter;
}

boolean suspendByID( int id )
{
    // loop over all threads in array:
    for( myThread t : threads )
    {
        if( t.id == id && t.runnable == true )
        {
            t.runnable = false;
            return true;
        }
    }
    return false;
}

boolean unsuspendByID( int id ) 
{
    for( myThread t : threads )
    {
        if( t.id == id && t.runnable == false )
        {
            synchronized(t)
            {
                t.runnable = true;
                t.notify();
            }
            System.out.println("Thread "+id+" Unsuspended.");
            return true;
        }
    }
    return false;
}

boolean terminateByID( int id )
{
    for( myThread t : threads )
    {
        if( t.id == id && t.terminated == false )
        {
            synchronized(t)
            {
                t.terminated = true;
                if( !t.isAlive() )
                    t.notify();
            }
            System.out.println("Thread "+id+" terminated.");
            return true;
        }
    }
    return false;
}

void terminateAll() 
{
    for( myThread t : threads )
    {
        if( t.terminated == false )
        {
            synchronized(t)
            {
                t.terminated = true;
                if( !t.isAlive() )
                    t.notify();
            }
        }
    }
}
}

这是myThread类的逻辑:

And here is the logic of myThread Class:

class myThread extends Thread
{
        protected volatile boolean runnable     = false;
        protected volatile boolean terminated   = false;

        Logger log;


        myThread(String keyword, int id)
        {
            // constrcution work ..
            log             = new Logger();
        }

        @Override
        public  void run()
        {   
            synchronized(this)
            {
                while(!terminated )
                {
                    // do some work ... 

                    this.wait(10000);

                    if(runnable == false)
                    {
                        System.out.println("Thread "+id+" is suspended.");
                        this.wait();
                    }
                }

                log.close();
                System.out.println("Thread terminated.").
            }
        }
    }

我的问题有两个:

1)我在Control Class中控制线程的方法,这是正确/最佳的实践方法吗?

1) The way I control the threads in Control Class, is it the right/best practice way?

2)我想在我的GUI中覆盖formWindowClosing()方法,以便它终止所有这样的线程:

2) I want to override formWindowClosing() method in my GUI so that it terminates all threads like this:

private void formWindowClosing(java.awt.event.WindowEvent evt) {                                   

        // Terminate all threads and close all open files from Logger:
        ret.terminateAll();


        System.exit(0);

    } 

这也是最佳做法/正确的方法吗? 当前,当关闭JForm时,位于myThread类末尾的语句:

Is this also best practice/the right way? Currently, when the JForm is closed, the statements at the end of myThread class:

log.close();
System.out.println("Thread terminated.").

根本不执行. 我认为System.exit(0)应该等待所有线程完成运行/终止,但是如何?

are not executed at all. I think System.exit(0) should wait until all threads finish running/terminated, but how??

推荐答案

请考虑使用interrupt()而不是notify().如果您的线程工作量涉及I/O,则中断它会使这些操作尽早终止.

Consider using interrupt() instead of notify(). If your thread workload involves I/O, interrupting it will get those operations to terminate early.

这篇关于如何处理JFrame中多个Java线程的终止和暂停?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆