Follow these steps to implement the example:
- Create a class named MyThread that extends the Thread class:
public class MyThread extends Thread {
- Declare three private Date attributes named creationDate, startDate, and finishDate:
private final Date creationDate;
private Date startDate;
private Date finishDate;
- Implement a constructor of the class. It receives the name and the Runnable object to be executed as parameters. Initialize the creation date of the thread:
public MyThread(Runnable target, String name ){
super(target,name);
creationDate = new Date();
}
- Implement the run() method. Store the start date of the thread, call the run() method of the parent class, and store the finish date of the execution:
@Override
public void run() {
setStartDate();
super.run();
setFinishDate();
}
- Implement a method to establish the value of the startDate attribute:
public synchronized void setStartDate() {
startDate=new Date();
}
- Implement a method to establish the value of the finishDate attribute:
public synchronized void setFinishDate() {
finishDate=new Date();
}
- Implement a method named getExecutionTime() that calculates the execution time of the thread as the difference between start and finish dates:
public synchronized long getExecutionTime() {
return finishDate.getTime()-startDate.getTime();
}
- Override the toString() method to return the creation date and execution time of the thread:
@Override
public synchronized String toString(){
StringBuilder buffer=new StringBuilder();
buffer.append(getName());
buffer.append(": ");
buffer.append(" Creation Date: ");
buffer.append(creationDate);
buffer.append(" : Running time: ");
buffer.append(getExecutionTime());
buffer.append(" Milliseconds.");
return buffer.toString();
}
- Create a class named MyThreadFactory that implements the ThreadFactory interface:
public class MyThreadFactory implements ThreadFactory {
- Declare a private AtomicInteger attribute named counter:
private AtomicInteger counter;
- Declare a private String attribute named prefix:
private String prefix;
- Implement the constructor of the class to initialize its attributes:
public MyThreadFactory (String prefix) {
this.prefix=prefix;
counter=new AtomicInteger(1);
}
- Implement the newThread() method. Create a MyThread object and increment the counter attribute:
@Override
public Thread newThread(Runnable r) {
MyThread myThread=new MyThread(r,prefix+"-"+counter
.getAndIncrement());
return myThread;
}
- Create a class named MyTask that implements the Runnable interface. Implement the run() method. Put the current thread to sleep for 2 seconds:
public class MyTask implements Runnable {
@Override
public void run() {
try {
TimeUnit.SECONDS.sleep(2);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
- Implement the main class of the example by creating a class named Main with a main() method:
public class Main {
public static void main(String[] args) throws Exception {
- Create a MyThreadFactory object:
MyThreadFactory myFactory=new MyThreadFactory
("MyThreadFactory");
- Create a Task object:
MyTask task=new MyTask();
- Create a MyThread object to execute the task using the newThread() method of the factory:
Thread thread=myFactory.newThread(task);
- Start the thread and wait for its finalization:
thread.start();
thread.join();
- Write information about the thread using the toString() method:
System.out.printf("Main: Thread information. ");
System.out.printf("%s ",thread);
System.out.printf("Main: End of the example. ");