Tuesday, September 16, 2008

A great Thread example from Nikos Pugunias(http://nikojava.wordpress.com/)

Hi Zhou!
You should be 100% certain that wait() is a method of Object.Consider 2 threads: Thread A carries out a loooong task. Thread B must wait for A's result to go on with its job.This trick between threads happens with a single object that both threads can access: a token. it's like the piece of wood that the athletes running 4x100m in the Olympics: Runner 1 starts, Runner 2 waits for the token from Runner 1, as soon as he gets it Runner 2 starts and so on...

//Here's the first runner,
class Runner1 extends Thread { }

//that holds a wooden stick.

class Runner1 extends Thread {
Object stick;
public Runner1(Object stick) { this.stick = stick; }
}


//This runner has a loooong task to do,

class Runner1 extends Thread {
Object stick;
public Runner1(Object stick) { this.stick = stick; }
public void run() { // perform a loooooong task here }
}


//and then he will give the token to another runner.

class Runner1 extends Thread {
Object stick;
public Runner1(Object stick) { this.stick = stick; }
public void run() {
synchronized (stick) {
// perform a loooooong task here stick.notify();
}
}
}



//Here's the second runner
class Runner2 extends Thread { }

//that can also receive a wooden stick.

class Runner2 extends Thread {
Object stick;
public Runner2(Object stick) { this.stick = stick; }
}



//This second runner has also something to do,

class Runner2 extends Thread {
Object stick;
public Runner2(Object stick) { this.stick = stick; }
public void run() {
// I want to do something after
// the first runner has finished.
}
}



// but he'll have to wait for the token first.

class Runner2 extends Thread {
Object stick;
public Runner2(Object stick) { this.stick = stick; }
public void run() {
// wait for Runner1
synchronized (stick) {
try { stick.wait();
} catch (InterruptedException e) { }
}
// I want to do something
//after the first runner has finished.
}
}



//+++++++++++++++++++++++++++
//Here's the complete code.
//+++++++++++++++++++++++++++

public class Run {
public static void main(String[] args) {
Object token = new Object();
new Runner1(token).start();
new Runner2(token).start();
}
}

class Runner1 extends Thread {
Object stick;
public Runner1(Object stick) { this.stick = stick; }
public void run() {
synchronized (stick) {
// perform a loooooong task here stick.notify();
}
}
}

class Runner2 extends Thread {
Object stick;
public Runner2(Object stick) { this.stick = stick; }
public void run() {
// wait for Runner1
synchronized (stick) {
try { stick.wait();
} catch (InterruptedException e) { }
}
// at this point Runner1 is guaranteed
// to have finished his work
}
}



//*************** Conclusion: ***************//
Using a single object, two threads may schedule in what order they will run in accuracy.In the question you have posted, there two threads: Job and main. They schedule themselves based on the [B]thread[B] instance.It's just a choice to use this instance instead of any other object.Review:When thread A calls token.notify() it says: You may take the token.When thread B calls token.wait() it is waiting to take the token.Always synchronize on the token.

Saturday, September 13, 2008

First Java step: set classpath

From website: http://mindprod.com/jgloss/classpath.html

The CLASSPATH is an environment variable that tells the Java compiler javac.exe where to look for class files to import or java.exe where to find class files to interpret.

In contrast, the PATH is an environment variable that tells the command processor the where to look for executable files, e.g. *.exe, *.com and *.bat files. The Classpath is one of the most confusing things in Java. Unfortunately, you must master it to even compile HelloWorld.

CLASSPATH And File Naming RecipesHere are my simplified rules for using CLASSPATH and naming the files on the javac.exe and java.exe command line:

  1. Put every class in a package. Don’t use the default package.

  2. Use the latest JDK. It will be the one everyone you ask help from is familiar with. Two dangling prepositions in one sentence. Churchill would be proud.
  3. Configure your SET CLASSPATH= in the environment to clear it out.
  4. You don’t need to explicitly include rt.jar:
    rt.jar system class files :
    in C:\Program Files\java\jre1.6.0_07\lib\rt.jar in the JRE 1.6.0 on your local hard disk.
    in J:\Program Files\java\jdk1.6.0_07\jre\lib\rt.jar in the in the JDK 1.6.0 on your local hard disk.
    in C:\Program Files\java\jre1.5.0_16\lib\rt.jar in the older JRE 1.5.0 on your local hard disk.
    in J:\Program Files\java\jdk1.5.0_16\jre\lib\rt.jar in the in the older JDK 1.5.0 on your local hard disk.

unless you are using Jikes, where you need to add it to the JIKESPATH, but not the CLASSPATH.

In all that follows, everything is strictly case sensitive.

  1. To compile a HelloWorld.java app in the default package in C:\MyDir, use cd \MyDir
    javac.exe -classpath . HelloWorld.java
  2. To run a HelloWorld.class app, in the default package in C:\MyDir, use cd \MyDir
    java.exe -classpath . HelloWorld
  3. To compile a HelloWorld.java app in C:\com\mindprod\mypackage, in package com.mindprod.mypackage, use c:
    cd \
    javac.exe -classpath . com\mindprod\mypackage\HelloWorld.java
    rem alternatively
    e:
    cd E:\com\mindprod\mypackage
    javac.exe -classpath .;C:\ HelloWorld.java
  4. To run a HelloWorld.class app in C:\com\mindprod\mypackage, in package com.mindprod.mypackage, use c:
    cd \
    rem not you do not do cd \com\mindprod\mypackage!!
    java.exe -classpath . com.mindprod.mypackage.HelloWorld
  5. To compile a HelloWorld.class app in C:\com\mindprod\mypackage, in into a jar called helloworld.jar in package com.mindprod.mypackage, use
  6. To run a HelloWorld.class app in C:\com\mindprod\mypackage, in a jar called helloworld.jar in package com.mindprod.mypackage, use CD \dirwherejaris
    java.exe -classpath helloworld.jar com.mindprod.mypackage.HelloWorld
    rem or if your Main-Class manifest points to HelloWorld.class.
    java.exe -jar helloworld.jar
    rem or if your *.jar extension is set to invoke java.exe
    helloworld.jar
    If for any reason the examples shown do not work with your version of java.exe, try replacing the \ in com\mindprod\mypackage\HelloWorld with / mypackage/HelloWorld.If you stare long enough at those examples, you may understand the logic behind them, and then you can create variants. If you can’t, just slavishly copy the closest matching example.

Friday, September 12, 2008

Java.io.Console

Make easy to accept input from and output to command line.


import java.io.Console;
Console c = System.console();
String readLine()
char[] readPassword(): for security purpose

== vs. equals() in Java

In JAVA, you may be confused with "==" and equals() all the time. What are they all about?

"==":
For primitive: check the velue

For String:
1.if see any new(means String s = new String("xxx"); -->check if they pointing to same object
2.if see only ""(means String s = "xxx"; ) -->check the string content

For Wrapper:
1. same as String, see any new --> check if they pointing to same object
2, if there is not, then tricky things coming:
2.1, if it is byte, short, int and the value is <=127, -->check the value
2.2, otherwise check the if they pointing to same object

equals():
For primitive: no way to use this

For general Object:
1.must override it to compare the key attribute(s) of object;
2,if collection is Hashxxx, you need override hashCode() too, otherwise equals won't effect

For String and wrappers:
they have well overridden equals() and hashCode()

Start to blog all my techs articles

I am always thinking to put all my tech related articles on a seperate blog. So from now on, I start to put all my research, study issues, results on this blog.