Basics of threading in Java.
The only object that you need to be familiar with to understand basic threading in Java is the Thread object. The basic method of using it is to instantiate it with a class that implements the Runnable interface. The following object illustrates how this would work:public class ThreadTest implements Runnable
{
private Thread thread;
public ThreadTest()
{
thread = new Thread(this);
thread.start(); //Begin executing the thread
}
public void run()
{
int counter = 0;
try
{
while (counter < 10000)
{
System.out.println(counter);
Thread.sleep(5000);
counter += 5000;
}
} catch (Exception ex) {}
System.out.println("Finished");
}
public static void main(String[] args) { new ThreadTest(); }
}
The basic model is that you create the thread instance, attach an instance of an object that implements the Runnable interface to it and then call the start() method. The start() method will call the object's run() method which is the code that will be executed while the thread is running.
Basic thread safety is implemented through the synchronized keyword. This keyword causes the other threads to block until the synchronized code is finished executing. Many collections, such as Hashtables, in Java are thread-safe collections, meaning that their accessor methods are synchronzied. For situations that this does not matter, other ones such as ArrayList work just fine. The JavaDocs contain information on which collections are thread-safe and which ones are not.
Java's synchronized keyword is an example of the monitor concept. They are similar to semaphores, but are a language-level construct that takes away a lot of the burden from the programmer. It is important that they be used sparingly because when they are not used properly, they can easily cause major performance problems.
Process Management
The Java Runtime object provides an easy, semi-Unix way of creating and executing processes. It's very similar to what one might find in the fork()/execvp() examples that are common for people starting out with writing multi-process and multithreaded Unix applications. Here's an example: Runtime run = Runtime.getRuntime();
Process proc = run.exec(new String[]{"notepad.exe"});
BufferedReader reader = new BufferedReader(new InputStreamReader(proc.getInputStream()));
That example will only run on Windows, but it will create a new Notepad window on the screen when run from the command line. The array that is passed to the exec method is the same thing that one would use on the command line to start the program. Notepad.exe in the case of notepad. However, execution of DOS programs can be tricky. They need to be executed within the command shell first, and Java does not start one by default. So, the array line will look like this:
Process proc = run.exec(new String[]{"cmd.exe", "/c", "dir", "C:\\windows"});
For an article describing some of the pitfalls of Java process management, read this.
**UPDATE**: Apparently, this post is REALLY popular with spammers. I have finally just given up and have closed off the comments and trackbacks because of the disproportionate number of spam hits this gets compared to the rest of my blog.
The budget hotel chain Travelodge has announced it is to remove pay-per-view adult TV content from its 20,000 in room tel
evisions – the first major chain to do so. Instead, Travelodge will commence a 10 m rollout of new flat screen digital TV sets relying on content from the DTT platform Freeview.
“We have an ever increasing number of families staying with us and it’s appropriate that we remove adult TV. Our other customers tell us that they would prefer to use a hotel without adult content available so we have responded to free porno clips needs, said Travelodge’s chief operating officer, Guy Parsons.
Adult porn has in the past been a lucrative source of income for major hotel chains that purposefully limit the number of available multichannel services. However, the UK market is held by just two companies that pay the chains only 10% of the transaction fee.
Hello folks. I stumbled upon this site and I'm just introducing myself.