Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Put Java Threading Class into a separate class

Consider following SWT code example:

http://dev.eclipse.org/viewcvs/index.cgi/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet151.java?view=co

How can I separate the inline defined class?

Thread thread = new Thread() {
        public void run() {
            ...
        }
    };

I want to define a separate class which updates the table just like it does here. How do I pass the list back to the table? Example code?

like image 785
Ta Sas Avatar asked Mar 07 '26 23:03

Ta Sas


1 Answers

Just create a class which extends Thread.

public class Task extends Thread {
    public void run() {
        // ...
    }
}

and create it as follows:

Task task = new Task();

The normal practice is however to implement Runnable:

public class Task implements Runnable {
    public void run() {
        // ...
    }
}

Or if you want a Thread which returns a result, implement Callable<T> where T represents the return type.

public class Task implements Callable<String> {
    public String call() {
        // ...
        return "string";
    }
}

Both can be executed using the ExecutorService.

like image 91
BalusC Avatar answered Mar 09 '26 11:03

BalusC



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!