public class Sample extends Panel {
// Constructor
public Sample() throws IOException {
/* JobOne and JobTwo need to be implemented 开发者_运维问答using Thread at same time */
JobOne{
//statements
}
JobTwo{
//statements
}
}
public static void main(String[] args) throws IOException {
new Sample();
}
}
How to perform this, if need these things to be implemented inside the constructor?
Don't extend Thread, implement java.lang.Runnable. That way you can extend from another class.
You can run it in its own thread like:
Thread thread = new Thread(myRunnable).start();
Also this way the Runnable isn't locked into being run as an individual thread, it can be handed to an Executor.
See this article on safe construction techniques for why you shouldn't start a thread from a constructor.
精彩评论