Blogged with Flock
Thursday, October 18, 2007
Sınır Ötesi Operasyon
Thursday, October 4, 2007
Volatile Variables In JAVA
Java Memory model defines the possible rules for threads and the expected behavior of multi-threaded programs so that programmers can design their programs accordingly. JSR-133 describes the semantics of threads, locks, volatile variables and data races.
Volatile variables are treated different from other variables. When you mark a variable as volatile, this warns the compiler to get fresh copies of the variable rather than caching the variable in the registers.
Volatile variables can be used to store shared variables at a lower cost than that of synchronization, but they have limitations. While writes to volatile variables are guaranteed to be immediately visible to other threads (because JVM does not allow threads to cache volatile variables), there is no way to render a read-modify-write sequence of operations atomic, meaning, for example, that a volatile variable cannot be used to reliably implement a mutex (mutual exclusion lock) or a counter.
Let's demonstrate this with a counter example. In the example a volatile counter variable will be incremented by multiple threads simultaneously. Below is the main class that holds the volatile variable and starts the threads.
|
Here is our example thread source code:
|
As a result: Volatile variables are best for use when there is only one thread that makes writes (updates volatile variable) and N threads that make reads. Reader threads get the most recent value of the volatile parameter when they perform a read.
Expected counter value : 100000
Result : 85751
Friday, September 14, 2007
Generating unique strings with JAVA
If you need randomly generated Strings in your java code you can use the below functions.
|
The classical hexEncode method:
|
Suppose that you need to generate unique random session ids for your logged in users. You can use the above function as follows :
String sessionId = generateRandomString(username);
10 consecutive calls to
generateRandomString("ilkinulas") generates the following strings:
38aca43c835888bc38fbb2d431f537489d637427
8c9fec7eb1309cd60a9548a11b16a30c2cf277a4
d495e83405704d38bfdf1bc3e2a38b31a5b52243
e3f15b900a3b80936596dc4471c0e42889dbf00c
83a4c869593af633a97b3b68433fac19bac7937d
f7573e8fdb4ecf6f58062570b66943b08d87fab8
c19bfdf75abbafbb5d9ca0ce37b90e97f08fccfa
41f0d9cd01ff814f8879bbff4b06642fc01a4624
aae1a792389239ca8db35a1ff9b16856c9921845
c2c9041ab5039b38c79a125c57a10cc6a3eabad9
Friday, September 7, 2007
The power off shell
Linux shell is a powerful tool for developers. Basic commands such as find, grep, diff can be combined to make our lives easier. Suppose that we are working on a project that uses CVS as source code control system, and there are plenty of versions of the same project on CVS. How can you find out the changed java files between version V1 and version V2? The answer is simple: use cvs and diff commands.
First, checkout the two versions of the project in to two different directories. Create a directory named v1. Checkout a version of the source into directory v1.
mkdir v1
cd v1
cvs -d :pserver:ilkinba@192.168.43.12:/usr/local/cvsroot/ina co -rICU_01_03_01 common/InoxCommonUtil
Create a directory named v2. Checkout a version of the source into directory v2.
mkdir v2
cd v2
cvs -d :pserver:ilkinba@192.168.43.12:/usr/local/cvsroot/ina co -rICU_01_03_02 common/InoxCommonUtil
Now it is time to use diff command.
diff -r -q v1/ v2/
This command outputs all the files that differ in directory v1 and v2.
"r" option is used to tell diff command to work recursively.
"q" option is used to tell diff command to work briefly. diff will only write the names of the different files.
Use grep to filter java source files.
diff -r -q v1/ v2/ | grep .java
After detecting the changed java source files you can use diff to see what has been changed in the java source files.
$ diff -r -q v1 v2 |grep .java
Files v1/common/InoxCommonUtil/code/src/com/oksijen/inox/common/util/edr/EDRGenerator.java and v2/common/InoxCommonUtil/code/src/com/oksijen/inox/common/util/edr/EDRGenerator.java differ
$ diff -y --suppress-common-lines v1/common/InoxCommonUtil/code/src/com/oksijen/inox/common/util/edr/EDRGenerator.java v2/common/InoxCommonUtil/code/src/com/oksijen/inox/common/util/edr/EDRGenerator.java
The result of the above command will be something like:
&& st[i].indexOf(label) >= 0) | && st[i].indexOf(fileName) >=
"y" option tells diff to output results in two columns(side by side)
"--suppress-common-lines" option is used to display only changed lines. Common lines will not be displayed.
Thats all.
Tuesday, September 4, 2007
Search java class files in jars.
This script helps you find the jar file which contains specified java class file.
Usage is simple :
./findClass /home/ilkinulas/lib javax.xml.ws.WebFault
This command searchs all the jars in directory /home/ilkinulas/lib and lists the jars that contain javax.xml.ws.WebFault java class.
#!/bin/bash
START_DIR=$1;
KEYWORD=$2;
for i in `find $START_DIR -name *.jar`
do
(jar tf $i | grep -q $KEYWORD) && echo $i;
done
