// 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");
Wednesday, June 24, 2009
How to Determine If the Current Thread Is Holding a Synchronized Lock
8:21 PM
Posted by
Techsavy
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
}
}
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
8:21 PM
Posted by
Techsavy
try {
long delay = 7000; // 7 seconds
Thread.sleep(delay);
} catch (InterruptedException e) {
}
long delay = 7000; // 7 seconds
Thread.sleep(delay);
} catch (InterruptedException e) {
}
How to Determine When a Thread Has Finished
8:20 PM
Posted by
Techsavy
// Create and start a thread
Thread thread = new SampleThread();
thread.start();
if (thread.isAlive()) {
// Thread is still running
} else {
// Thread is finished
}
Thread thread = new SampleThread();
thread.start();
if (thread.isAlive()) {
// Thread is still running
} else {
// Thread is finished
}
How to Stop a Thread
8:18 PM
Posted by
Techsavy
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...
}
}
}
// 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
8:17 PM
Posted by
Techsavy
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();
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
8:16 PM
Posted by
Techsavy
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) {
}
// 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
8:15 PM
Posted by
Techsavy
// 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);
}
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
8:14 PM
Posted by
Techsavy
// Get a system property
String dir = System.getProperty("SampleProperty");
// Set a system property
String previousValue = System.setProperty("SampleProperty", "newValue");
String dir = System.getProperty("SampleProperty");
// Set a system property
String previousValue = System.setProperty("SampleProperty", "newValue");
Subscribe to:
Posts (Atom)