How to do it...

Follow these steps to implement the example:

  1. Create a class named MyThread that extends the Thread class:
        public class MyThread extends Thread {
  1. Declare three private Date attributes named creationDate, startDate, and finishDate:
        private final Date creationDate; 
private Date startDate;
private Date finishDate;
  1. 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();
}
  1. 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();
}
  1. Implement a method to establish the value of the startDate attribute:
        public synchronized void setStartDate() { 
startDate=new Date();
}
  1. Implement a method to establish the value of the finishDate attribute:
        public synchronized void setFinishDate() { 
finishDate=new Date();
}
  1. 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();
}
  1. 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();
}
  1. Create a class named MyThreadFactory that implements the ThreadFactory interface:
        public class MyThreadFactory implements ThreadFactory {
  1. Declare a private AtomicInteger attribute named counter:
        private AtomicInteger counter;
  1. Declare a private String attribute named prefix:
        private String prefix;
  1. Implement the constructor of the class to initialize its attributes:
        public MyThreadFactory (String prefix) { 
this.prefix=prefix;
counter=new AtomicInteger(1);
}
  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;
}
  1. 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();
}
}
}
  1. 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 {
  1. Create a MyThreadFactory object:
        MyThreadFactory myFactory=new MyThreadFactory
("MyThreadFactory");
  1. Create a Task object:
        MyTask task=new MyTask();
  1. Create a MyThread object to execute the task using the newThread() method of the factory:
        Thread thread=myFactory.newThread(task);
  1. Start the thread and wait for its finalization:
        thread.start(); 
thread.join();
  1. 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. ");
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.144.56