Snoop to file
# snoop -xv -o snoopfilename port portNo
Read from file with Snoop
# snoop -xv -i snoopfilename port portNo
TcpDump
# tcpdump -i eth0 -s 0 -w snoopfilename port portNo
Tuesday, August 24, 2010
Wednesday, August 18, 2010
Vector vs ArrayList vs HashSet
List syncList = Collections.synchronizedList(new ArrayList());
Set syncSet = Collections.synchronizedSet(new HashSet());
Vector is naturally synchronized.
Performance Test Result
#1 :ArrayList
#2 :Vector
#3 : HashSet
Read more: Java Code Geeks: Java Best Practices – Vector vs ArrayList vs HashSet
http://www.javacodegeeks.com/2010/08/java-best-practices-vector-arraylist.html#ixzz0wxSNUeuS
Set syncSet = Collections.synchronizedSet(new HashSet());
Vector is naturally synchronized.
Performance Test Result
#1 :ArrayList
#2 :Vector
#3 : HashSet
Read more: Java Code Geeks: Java Best Practices – Vector vs ArrayList vs HashSet
http://www.javacodegeeks.com/2010/08/java-best-practices-vector-arraylist.html#ixzz0wxSNUeuS
Saturday, August 14, 2010
Scalability hints
Avoid lock on static variables or static methods:
It locks your class. Even worse than locking your object!
private static Object myVar = null;
private foo{
synchronized (myVar){// do not do that
....
}
}
private static synchronized myMethod(){
.....
}
Use lock free data structures
Use java.util.concurrent.atomic package.It uses lock-free algorithms under the help with hardware synchronization primitives without using native code.
Reduce lock granularity:
Try to prefer "block locks" instead of "method locks" if possible.synchronized methods locks the Object instance as you know.
public class SchemaManager {
private HashMap schema;
private HashMap treeNodes;
//Bad way!
public boolean synchronized updateSchema(HashMap nodeTree) {
String nodeName = (String)nodeTree.get("nodeName");
String nodeAttributes = (List)nodeTree.get("attributes");
if (nodeName == null)
return false;
else
return schema.update(nodeName,nodeAttributes);
}
//Good Way
public boolean updateSchema(HashMap nodeTree) {
String nodeName = (String)nodeTree.get("nodeName");
String nodeAttributes = (List)nodeTree.get("attributes");
synchronized (schema) {
if (nodeName == null)
return false;
else
return schema.update(nodeName,nodeAttributes); }
}
public boolean synchronized updateTreeNodes() { ...... } }
Make synchronized blocks as short as possible
Move the thread safe code outside of the synchronized block.
public boolean updateSchema(HashMap nodeTree) {
synchronized (schema) {
String nodeName = (String)nodeTree.get("nodeName");
String nodeAttributes = (List)nodeTree.get("attributes");
if (nodeName == null)
return false;
else
return schema.update(nodeName,nodeAttributes);
}
}
public boolean updateSchema(HashMap nodeTree) {
String nodeName = (String)nodeTree.get("nodeName");
String nodeAttributes = (List)nodeTree.get("attributes");
synchronized (schema) {
if (nodeName == null)
return false;
else
return schema.update(nodeName,nodeAttributes);
}
}
Try to use ThreadLocal variables if possible.
It locks your class. Even worse than locking your object!
private static Object myVar = null;
private foo{
synchronized (myVar){// do not do that
....
}
}
private static synchronized myMethod(){
.....
}
Use lock free data structures
Use java.util.concurrent.atomic package.It uses lock-free algorithms under the help with hardware synchronization primitives without using native code.
Reduce lock granularity:
Try to prefer "block locks" instead of "method locks" if possible.synchronized methods locks the Object instance as you know.
public class SchemaManager {
private HashMap schema;
private HashMap treeNodes;
//Bad way!
public boolean synchronized updateSchema(HashMap nodeTree) {
String nodeName = (String)nodeTree.get("nodeName");
String nodeAttributes = (List)nodeTree.get("attributes");
if (nodeName == null)
return false;
else
return schema.update(nodeName,nodeAttributes);
}
//Good Way
public boolean updateSchema(HashMap nodeTree) {
String nodeName = (String)nodeTree.get("nodeName");
String nodeAttributes = (List)nodeTree.get("attributes");
synchronized (schema) {
if (nodeName == null)
return false;
else
return schema.update(nodeName,nodeAttributes); }
}
public boolean synchronized updateTreeNodes() { ...... } }
Make synchronized blocks as short as possible
Move the thread safe code outside of the synchronized block.
public boolean updateSchema(HashMap nodeTree) {
synchronized (schema) {
String nodeName = (String)nodeTree.get("nodeName");
String nodeAttributes = (List)nodeTree.get("attributes");
if (nodeName == null)
return false;
else
return schema.update(nodeName,nodeAttributes);
}
}
public boolean updateSchema(HashMap nodeTree) {
String nodeName = (String)nodeTree.get("nodeName");
String nodeAttributes = (List)nodeTree.get("attributes");
synchronized (schema) {
if (nodeName == null)
return false;
else
return schema.update(nodeName,nodeAttributes);
}
}
Try to use ThreadLocal variables if possible.
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
We collect all dependencies under a directory and it is enough to say everthing is out there...
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
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 !
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 !
Subscribe to:
Posts (Atom)