Wednesday, June 24, 2009

How to Create an ArrayList


// Create the list
List list = new ArrayList();
// Append an element to the list
list.add("abc");
// Insert an element at the head of the list
list.add(0, "xyz");

How to Determine If the Current Thread Is Holding a Synchronized Lock


public synchronized void myMethod() {
boolean hasLock = false;
Object obj = new Object();

// Determine if current thread has lock for obj
hasLock = Thread.holdsLock(obj); // false
synchronized (obj) {
hasLock = Thread.holdsLock(obj); // true
}
}

How to Pause the Current Thread


try {
long delay = 7000; // 7 seconds
Thread.sleep(delay);
} catch (InterruptedException e) {
}

How to Determine When a Thread Has Finished


// Create and start a thread
Thread thread = new SampleThread();
thread.start();

if (thread.isAlive()) {
// Thread is still running
} else {
// Thread is finished
}

How to Stop a Thread


There are two methods(stop() and suspend()) available in Thread class to stop the thread. As these methods are deprecated, it is advisable not to use these methods and use below mentioned approach to stop the thread.

// Create and start the thread
SampleThread thread = new SampleThread();
thread.start();
//...Some Code...
// Stop the thread
thread.done = true;

class SampleThread extends Thread {
boolean done = false;
// This method is called when the thread runs
public void run() {
while (true) {
// ...Some Code...
if (done) {
return;
}
// ...Some Code...
}
}
}

How to Create a Thread


There are two ways to create a thread.
1) Using Thread class
2) Using Runnable interface

Using Thread class:

// This class extends Thread
class SampleThread extends Thread {
// This method is the entry point of thread. It is called when thread runs.
public void run() {
}
}

// Create and start the thread
Thread thread = new SampleThread();
thread.start();


Using Runnable interface:

class SampleThread implements Runnable {
// This method is called when the thread runs
public void run() {
}
}

// Create the object with the run() method
Runnable runnable = new SampleThread();

Thread thread = new Thread(runnable);

// Start the thread
thread.start();

How to Execute a Command


try {
// Execute a command. "ls -a" a unix command. So, this command will work on unix only.
String command = "ls -a";
Process child = Runtime.getRuntime().exec(command);

} catch (IOException e) {

}

How to List All System Properties


// Get all system properties
Properties props = System.getProperties();

// Enumerate all system properties
Enumeration enum = props.propertyNames();
for (; enum.hasMoreElements(); ) {
// Get property name
String propName = (String)enum.nextElement();

// Get property value
String propValue = (String)props.get(propName);
}

How to Get and Set the Value of a System Property


// Get a system property
String dir = System.getProperty("SampleProperty");

// Set a system property
String previousValue = System.setProperty("SampleProperty", "newValue");
 

Sample Java Codes Copyright © 2008 Green Scrapbook Diary Designed by SimplyWP | Made free by Scrapbooking Software | Bloggerized by Ipiet Notez