I was asked once in an interview How will you check the different status of thread ?
The question was to write a java code which will check which thread is waiting for which lock (if any) or if it dead etc.
Here is one way of doing it….
/**
* THIS CODE IS DISTRIBUTED "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*/
package com.mumz.test.thread.threadinfo;
/**
* @author prabhat.jha
*/
public class MyOwnThread implements Runnable {
public StringBuilder builder = new StringBuilder();
/**
* @param data
*/
public void addData(String data) {
builder.append(isValidString(data) ? data : "");
}
/**
* (non-Javadoc)
*
* @see java.lang.Runnable#run()
*/
public void run() {
synchronized (builder) {
if (builder.toString().length() > 10) {
try {
builder.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
if (builder.toString().length() > 10) {
String content = builder.toString().substring(0, 9);
builder.delete(0, builder.length() - 1);
builder.append(content);
}
if (builder.length() > 10) {
builder.notifyAll();
}
}
}
/**
* @param string
* @return
*/
private boolean isValidString(String string) {
return (string != null && !string.equals("") && string.trim().length() > 0);
}
}
Lets create thread and then check if they are waiting for lock to be acquired or else just print there status, here is the code snippet:
package com.mumz.test.thread.threadinfo;
/**
* THIS CODE IS DISTRIBUTED "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*/
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.lang.management.ManagementFactory;
import java.lang.management.ThreadInfo;
import java.lang.management.ThreadMXBean;
/**
* @author prabhat.jha
*
*/
public class ThreadFactory {
public static void main(String[] args) {
MyOwnThread myOwnThread = null;
Thread thread = null;
long[] threadIds = new long[20];
for (int iDx = 0; iDx < 20; iDx++) {
myOwnThread = new MyOwnThread();
myOwnThread.addData("Thread : " + iDx);
thread = new Thread(myOwnThread);
thread.setName(String.valueOf(iDx));
threadIds[iDx] = thread.getId();
thread.start();
}
ThreadFactory factory = new ThreadFactory();
factory.checkThreadStatus(threadIds);
}
/**
* @param threadIds
*/
private void checkThreadStatus(long[] threadIds) {
ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();
ThreadInfo[] threadInfos = threadMXBean.getThreadInfo(threadIds);
StringBuilder info = new StringBuilder();
for (ThreadInfo threadInfo : threadInfos) {
if (threadInfo == null) {
continue;
}
info.append("threadInfo.getThreadId() : " + threadInfo.getThreadId());
info.append("\n");
info.append("threadInfo.getThreadName() : " + threadInfo.getThreadName());
info.append("\n");
info.append("threadInfo.getThreadState() : " + threadInfo.getThreadState());
info.append("\n");
info.append("threadInfo.getLockName() : " + threadInfo.getLockName());
info.append("\n");
info.append("threadInfo.getLockOwnerId() : " + threadInfo.getLockOwnerId());
info.append("\n");
info.append("threadInfo.getLockOwnerName() : " + threadInfo.getLockOwnerName());
}
writeDataToFile(info.toString());
}
/**
* @param info
*/
private void writeDataToFile(String info) {
File file = new File("c://" + System.currentTimeMillis());
PrintWriter pw = null;
BufferedOutputStream bos = null;
try {
if (!file.exists()) {
file.createNewFile();
}
bos = new BufferedOutputStream(new FileOutputStream(file));
pw = new PrintWriter(bos);
pw.write(info);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
pw.close();
try {
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}