Socket clientSocket = serverSocket.accept(); Runnable connectionHandler = new ConnectionHandler(clientSocket); new Thread(connectionHandler).start();
runButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { // Some code checked on some radio buttons if(radioButton.isSelected()) { if(radioButton.getText().equals("VALIDATE")) { Runnable runnable = new ValidateThread(); Thread thread = new Thread(runnable); thread.start(); } } } });
ExecutorService threadPoolExecutor = Executors.newSingleThreadExecutor(); Runnable longRunningTask = new Runnable(); // submit task to threadpool: Future longRunningTaskFuture = threadPoolExecutor.submit(longRunningTask); ... ... // At some point in the future, if you want to kill the task: longRunningTaskFuture.cancel(true); ... ...
public class Main { private static final int NTHREDS = 10; public static void main(String[] args) { ExecutorService executor = Executors.newFixedThreadPool(NTHREDS); for (int i = 0; i < 500; i++) { Runnable worker = new MyRunnable(10000000L + i); executor.execute(worker); } // This will make the executor accept no new threads // and finish all existing threads in the queue executor.shutdown(); // Wait until all threads are finish executor.awaitTermination(); System.out.println("Finished all threads"); } }
Handler handler = new Handler() handler.postDelayed(new Runnable()
SwingUtilities.invokeLater(new Runnable() { @Override public void run() { lblStatus.setText(event.getData());
137 } 138 139 Toolkit.getToolkit().defer(new Runnable() { 140 @Override public void run() { 141 try {
${:import(javax.swing.SwingUtilities)} SwingUtilities.invokeLater(new Runnable() { @Override public void run() { ${cursor} } });
public void main(String[] args) { SwingUtilities.invokeLater(new Runnable( public void run() { program = new MainProg(); } )); }
public static void main( String[] args ) { Runnable t = new Superdaemon(); t.run(); }
protected void onHandleIntent(Intent intent) { synchronized (intent) { final Intent intentCpy=intent; new Thread(new Runnable() { @Override public void run() { //onHandleIntent code which is in question post } } }
InputStream fis = Files.newInputStream (sourcePath); OutputStream fos = Files.newOutputStream(sinkPath); final Runnable runnable = new Runner(fis, fos); final Thread thread = new Thread(runnable); thread.start();
import java.applet.Applet; public class OurApplet extends Applet { public void init() { Runnable ot = new OurClass(); Thread th = new Thread(ot); th.start(); } }
InputStream fis = Files.newInputStream (sourcePath); OutputStream fos = Files.newOutputStream(sinkPath); final Runnable runnable = new Runner(fis, fos); final Thread thread = new Thread(runnable); thread.start();
public class CustomApplet extends Applet { public void init() { Runnable ot = new OurClass(); Thread th = new Thread(ot); th.start(); } }
ScheduledExecutorService scheduledTaskExecutor = Executors.newScheduledThreadPool(1); Runnable longRunningTask = new Runnable(); // schedule long running task in 2 minutes: ScheduledFuture scheduleFuture = scheduledTaskExecutor.schedule(longRunningTask, 2, TimeUnit.MINUTES); ... ... // At some point in the future, if you want to cancel scheduled task: scheduleFuture.cancel(true); ... ...
Runnable r = new CrashOnAssertionError( new Runnable() { public void run() { // do stuff } }); new Thread(r).start();
@Stateless public class ReportBean { @Resource private ManagedThreadFactory threadFactory; public void runReports() { Thread thread = threadFactory.newThread(new Runnable()); thread.start(); }
// Here you are adding a "Fade" instance to the frame. frame.add(new Fade()); ... // Here you are creating a NEW "Fade" instance. Only in // this instance, the alpha value will be affected Runnable fade = new Fade(); executor.execute(fade);