简体   繁体   中英

how to make my program to wait for a particular thread and also not affecting my GUI's interactivity?

I have a program based on MVC which gets data from internet and start to parsing it and of course in networking programs we need to have threads.

I have a class for reading data from internet such below :

public class WebReader extends Thread
{


    private String result="";



    public String getResult()
    {
        return result;
    }
    public void setResult(String t)
    {
        result = t;
    }

    public WebReader(get args here)
    {
        //initializing 
    }

    public void getData()
    {
        //call the big getData(args);

    }
    public void getData(Sargs)
    {
        //magics happens here
    }

    @Override
    public void run() 
    {

        //call getData()
    }
}

and for using this class inside my model I've been using below code :

//inside a function
WebReader wr = new  WebReader(args);
        wr.start();
            wr.join();
            String s = wr.getResult();
            this.parseData(s);

I know by using join() I'm join the thread to the current thread and by doing this my GUI will stuck till complete result come from network. but I want to be sure that WebReader class has read the data first and then carry on what should I do for that?

SwingWorker is a built-in mechanism for executing long running tasks on a background thread so the EDT is not blocked. Depending on the overall design and intent of your application, you may find it easier than some of the alternative solutions.

Edit: I'm assuming your GUI is in Swing...? If not this answer won't apply.

Without giving you the exact solution, I can give you some things to consider. You will need to do something in the GUI to indicate the user should wait. This could be as little as a wait/busy cursor, or some sort of progress monitor. Implementing these is different depending on the GUI technology that you are using. When you use the notification mechanism to the user, it will have a means of waking up when the data arrives.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM