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

    Preventing Memery Locks on the Multi CPU machine

    Problem
       Multithreaded apps create new objects at the same time
       New objects are always created in the EDEN space
       During object creation, memory is locked
       On a multi CPU machine (threads run concurrently) there can be contention

    Solution
        Allow each thread to have a private piece of the EDEN
        space
    Thread Local Allocation Buffer
        -XX:+UseTLAB
        -XX:TLABSize=
        -XX:+ResizeTLAB
        (On by default on multi CPU machines and newer JDK)
    Analyse TLAB usage
        -XX:+PrintTLAB
    JDK 1.5 and higher (GC ergonomics)
        Dynamic sizing algorithm, tuned to each thread


    INSIDE THE JAVA VIRTUAL MACHINE / Filip Hanik