ThreadFactory(I) in JAVA

Here are some notes on ThreadFactory(I):

A Sample Programme:

package com.practice.thread;

import java.awt.List;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.concurrent.ThreadFactory;

class LogicThread implements Runnable{

@Override
public void run() {
// TODO Auto-generated method stub
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName());
}

}

class ThreadFactoryImpl implements ThreadFactory{
private String threadName;
public ThreadFactoryImpl(String threadName) {
super();
this.threadName = threadName;
this.listThreads = new ArrayList<String>();
}
private int counter=1;
private java.util.List<String> listThreads;

@Override
public Thread newThread(Runnable r) {
// TODO Auto-generated method stub
Thread t = new Thread(r, threadName + “-Thread_” + counter);
counter++;
listThreads.add(String.format(“Created thread %d with name %s on %s \n”, t.getId(), t.getName(), new Date()));
return t;
}

public String getThreadValues(){
StringBuffer stringBuffer=new StringBuffer();
Iterator<String> iterator=listThreads.iterator();
while(iterator.hasNext()){
stringBuffer.append(iterator.next());
}
return stringBuffer.toString();
}
}
public class ThreadFactoryExample {
public static void main(String[] args) {
ThreadFactoryImpl threadFactoryImpl=new ThreadFactoryImpl(“Thread_Factory”);
LogicThread logicThread=new LogicThread();
//Thread logic i.e sleep
Thread thread;
for(int i=0;i<10;i++){
thread=new Thread(logicThread);
thread=threadFactoryImpl.newThread(thread);
//Creating thread using Facoty Impl by using newThread()
thread.start();
}
System.out.println(threadFactoryImpl.getThreadValues());
}
}

 

Created thread 11 with name Thread_Factory-Thread_1 on Sun Jan 22 14:24:17 IST 2017
Created thread 13 with name Thread_Factory-Thread_2 on Sun Jan 22 14:24:17 IST 2017
Created thread 15 with name Thread_Factory-Thread_3 on Sun Jan 22 14:24:17 IST 2017
Created thread 17 with name Thread_Factory-Thread_4 on Sun Jan 22 14:24:17 IST 2017
Created thread 19 with name Thread_Factory-Thread_5 on Sun Jan 22 14:24:17 IST 2017
Created thread 21 with name Thread_Factory-Thread_6 on Sun Jan 22 14:24:17 IST 2017
Created thread 23 with name Thread_Factory-Thread_7 on Sun Jan 22 14:24:17 IST 2017
Created thread 25 with name Thread_Factory-Thread_8 on Sun Jan 22 14:24:17 IST 2017
Created thread 27 with name Thread_Factory-Thread_9 on Sun Jan 22 14:24:17 IST 2017
Created thread 29 with name Thread_Factory-Thread_10 on Sun Jan 22 14:24:17 IST 2017

Thread_Factory-Thread_1
Thread_Factory-Thread_3
Thread_Factory-Thread_2
Thread_Factory-Thread_4
Thread_Factory-Thread_9
Thread_Factory-Thread_5
Thread_Factory-Thread_6
Thread_Factory-Thread_7
Thread_Factory-Thread_8
Thread_Factory-Thread_10

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s