Skip to content

Instantly share code, notes, and snippets.

@paullewallencom
Created June 9, 2018 17:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save paullewallencom/c146da9c54c219ade29483e027714f68 to your computer and use it in GitHub Desktop.
Save paullewallencom/c146da9c54c219ade29483e027714f68 to your computer and use it in GitHub Desktop.
Waiting for the Finalization of a Thread
import java.util.Date;
import java.util.concurrent.TimeUnit;
public class DataSourcesLoader implements Runnable {
@Override
public void run() {
// Writes a messsage
System.out.printf("Begining data sources loading: %s\n",new Date());
// Sleeps four seconds
try {
TimeUnit.SECONDS.sleep(4);
} catch (InterruptedException e) {
e.printStackTrace();
}
// Writes a message
System.out.printf("Data sources loading has finished: %s\n",new Date());
}
}
// Waiting for the finalization of a thread
import java.util.Date;
public class Main {
public static void main(String[] args) {
// Creates and starts a DataSourceLoader runnable object
DataSourcesLoader dsLoader = new DataSourcesLoader();
Thread thread1 = new Thread(dsLoader,"DataSourceThread");
// Creates and starts a NetworkConnectionsLoader runnable object
NetworkConnectionsLoader ncLoader = new NetworkConnectionsLoader();
Thread thread2 = new Thread(ncLoader,"NetworkConnectionLoader");
// Start both threads
thread1.start();
thread2.start();
// Wait for the finalization of the two threads
try {
thread1.join();
thread2.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
// Waits a message
System.out.printf("Main: Configuration has been loaded: %s\n",new Date());
}
}
import java.util.Date;
import java.util.concurrent.TimeUnit;
public class NetworkConnectionsLoader implements Runnable {
@Override
public void run() {
// Writes a message
System.out.printf("Begining network connections loading: %s\n",new Date());
// Sleep six seconds
try {
TimeUnit.SECONDS.sleep(6);
} catch (InterruptedException e) {
e.printStackTrace();
}
// Writes a message
System.out.printf("Network connections loading has finished: %s\n",new Date());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment