Friday, August 13, 2010

Java 6 allows classpath wildcards

All we hate to complete our $CLASSPATH  !

We always try to setup a CLASSPATH enviroment variable while dealing with all & everytime & anywhere.

Fortunately Sun noticed our tears at the end :)

Now we can specify lib/*, and all of the JAR files listed in that directory (not recursively), in the classpath.

We collect all dependencies under a directory and it is enough to say everthing is out there...

Saturday, August 7, 2010

It is my blog :) It is not forbidden for me to share off topic pictures huh ?


Me
&
&
Tolga Karadeniz

Sunday, August 1, 2010

Reliable Multicasting with JGroups

Append the jgroups jar to your classpath.

Then execute the following code twice in different consoles like this.These are our chat clients.

java -Djava.net.preferIPv4Stack=true SimpleChat


import java.io.*;
import java.net.InetAddress;
import org.jgroups.*;

public class SimpleChat implements Receiver {
    JChannel channel;

    public static void main (String[] args) throws Exception {
        new SimpleChat ().start ();
    }

    private void start () throws Exception {
        channel = new JChannel ();
        channel.connect ("ChatCluster");
        channel.setReceiver (this);
        eventLoop ();
        channel.close ();
    }

    private void eventLoop () {
        BufferedReader in = new BufferedReader (new InputStreamReader (System.in));
        while ( true ) {
            try {
                System.out.print ("> ");
                System.out.flush ();
                String line = in.readLine ().toLowerCase ();
                if (line.startsWith ("quit") || line.startsWith ("exit")) {
                    break;
                }
                line = "[" + this + "] " + line;
                Message msg = new Message (null, null, line);
                channel.send (msg);
            } catch (Exception e) {
                e.printStackTrace ();
            }
        }
    }

    public void receive (Message message) {
        System.out.println ("message = " + new String (message.getBuffer ()));
    }

    public byte[] getState () {
        return new byte[0]; 
    }

    public void setState (byte[] bytes) {
        //To change body of implemented methods use File | Settings | File Templates.
    }

    public void viewAccepted (View view) {
        System.out.println ("view = " + view.toString ());
    }

    public void suspect (Address address) {
        System.out.println ("address = " + address);
    }

    public void block () {

    }
}

There are more fancy examples in there. Like org.jgroups.demos.Draw !

Sunday, July 18, 2010

Three ways to make an object thread-safe


There are basically three approaches you can take to make an object such as RGBThread thread-safe:

  1. Synchronize critical sections
  2. Make it immutable
  3. Use a thread-safe wrapper 
design techniques and thread safety

    Why worry about thread safety?

    Given the structure of the JVM, local variables, method parameters, and return values are inherently "thread-safe." But instance variables and class variables will only be thread-safe if you design your class appropriately

    -- Bill Venners