PATH=$PATH\:/dir/path/dir/path is the path you want to add to your $PATH
4 Jun 2012
Add PATH for MAC or Linux temporarily
25 Apr 2012
How to export all resources into runnable JAR file in Eclipse?
Well, the post title says it all.
I was stuck in doing that, but today I've found the solution.
For example, I have the directory tree like this:
In file "MainFrame.java" I want to display "home.png", so I used this code:
Of course, it works well when you compile in Eclipse, but then you export to runnable JAR file (File->Export->Java->Runnable JAR file), and try to run the JAR file with command line you will get the error like:
So what is the solution?
It's so simple, just change your code to (notice the bold texts):
Then try to export again, the exported JAR file should work well ;)
I was stuck in doing that, but today I've found the solution.
For example, I have the directory tree like this:
- src
- net.leolink.project_name.resources
- myicon.png
- net.leolink.project_name.client
- MainFrame.java
In file "MainFrame.java" I want to display "home.png", so I used this code:
ImageIcon icon = new ImageIcon(this.getClass().getResource("../resource/home.png"));
JLabel lb = new JLabel(icon);
Of course, it works well when you compile in Eclipse, but then you export to runnable JAR file (File->Export->Java->Runnable JAR file), and try to run the JAR file with command line you will get the error like:
Exception in thread "main" java.lang.NullPointerException
at javax.swing.ImageIcon.<init>(ImageIcon.java:181)
at net.leolink.project_name.client.MainFrame.<init>(Main.java:15)
at net.leolink.project_name.client.MainFrame.main(Main.java:27)
So what is the solution?
It's so simple, just change your code to (notice the bold texts):
ImageIcon icon = new ImageIcon(this.getClass().getClassLoader().getResource("net/leolink/project_name/resource/home.png"));
JLabel lb = new JLabel(icon);
Then try to export again, the exported JAR file should work well ;)
15 Apr 2012
Difference between volatile and synchronized
volatile is a field modifier, while synchronized modifies code blocks and methods. So we can specify three variations of a simple accessor using those two keywords: int i1; int geti1() {return i1;}
volatile int i2; int geti2() {return i2;}
int i3; synchronized int geti3() {return i3;}geti1() accesses the value currently stored in i1 in the current thread. Threads can have local copies of variables, and the data does not have to be the same as the data held in other threads. In particular, another thread may have updated i1 in it's thread, but the value in the current thread could be different from that updated value. In fact Java has the idea of a "main" memory, and this is the memory that holds the current "correct" value for variables. Threads can have their own copy of data for variables, and the thread copy can be different from the "main" memory. So in fact, it is possible for the "main" memory to have a value of 1 for i1, for thread1 to have a value of 2 for i1 and for thread2 to have a value of 3 for i1 if thread1 and thread2 have both updated i1 but those updated value has not yet been propagated to "main" memory or other threads. On the other hand,
geti2() effectively accesses the value of i2 from "main" memory. A volatile variable is not allowed to have a local copy of a variable that is different from the value currently held in "main" memory. Effectively, a variable declared volatile must have it's data synchronized across all threads, so that whenever you access or update the variable in any thread, all other threads immediately see the same value. Generally volatile variables have a higher access and update overhead than "plain" variables. Generally threads are allowed to have their own copy of data is for better efficiency. There are two differences between volitile and synchronized.
Firstly
synchronized obtains and releases locks on monitors which can force only one thread at a time to execute a code block. That's the fairly well known aspect to synchronized. But synchronized also synchronizes memory. In fact synchronized synchronizes the whole of thread memory with "main" memory. So executing geti3() does the following: - The thread acquires the lock on the monitor for object
this. - The thread memory flushes all its variables, i.e. it has all of its variables effectively read from "main" memory .
- The code block is executed (in this case setting the return value to the current value of
i3, which may have just been reset from "main" memory). - (Any changes to variables would normally now be written out to "main" memory, but for
geti3()we have no changes.) - The thread releases the lock on the monitor for object
this.
volatile only synchronizes the value of one variable between thread memory and "main" memory, synchronized synchronizes the value of all variables between thread memory and "main" memory, and locks and releases a monitor to boot. Clearly synchronized is likely to have more overhead than volatile.Source: http://javaexp.blogspot.com/2007/12/difference-between-volatile-and.html
Labels:
Java
Do you ever use the "volatile" keyword in Java?
I haven't ever :( I even haven't ever heard about it before reading this post on stackoverflow.com: http://stackoverflow.com/questions/698964/checking-if-a-clientsocket-has-disconnected-in-java-hangs
And here is something I've found on the net:
And here is something I've found on the net:
- http://stackoverflow.com/questions/106591/do-you-ever-use-the-volatile-keyword-in-java
- http://docs.oracle.com/javase/specs/jls/se7/html/jls-8.html#jls-8.3.1.4
8.3.1.4.
volatileFieldsThe Java programming language allows threads to access shared variables (§17.1). As a rule, to ensure that shared variables are consistently and reliably updated, a thread should ensure that it has exclusive use of such variables by obtaining a lock that, conventionally, enforces mutual exclusion for those shared variables.The Java programming language provides a second mechanism,volatilefields, that is more convenient than locking for some purposes.A field may be declaredvolatile, in which case the Java Memory Model ensures that all threads see a consistent value for the variable (§17.4).
Example 8.3.1.4-1.volatileFieldsIf, in the following example, one thread repeatedly calls the methodone(but no more thanInteger.MAX_VALUEtimes in all), and another thread repeatedly calls the methodtwo:class Test { static int i = 0, j = 0; static void one() { i++; j++; } static void two() { System.out.println("i=" + i + " j=" + j); } }then methodtwocould occasionally print a value forjthat is greater than the value ofi, because the example includes no synchronization and, under the rules explained in §17.4, the shared values ofiandjmight be updated out of order.One way to prevent this out-or-order behavior would be to declare methodsoneandtwoto besynchronized(§8.4.3.6):class Test { static int i = 0, j = 0; static synchronized void one() { i++; j++; } static synchronized void two() { System.out.println("i=" + i + " j=" + j); } }This prevents methodoneand methodtwofrom being executed concurrently, and furthermore guarantees that the shared values ofiandjare both updated before methodonereturns. Therefore methodtwonever observes a value forjgreater than that fori; indeed, it always observes the same value foriandj.Another approach would be to declareiandjto bevolatile:class Test { static volatile int i = 0, j = 0; static void one() { i++; j++; } static void two() { System.out.println("i=" + i + " j=" + j); } }This allows methodoneand methodtwoto be executed concurrently, but guarantees that accesses to the shared values foriandjoccur exactly as many times, and in exactly the same order, as they appear to occur during execution of the program text by each thread. Therefore, the shared value forjis never greater than that fori, because each update toimust be reflected in the shared value foribefore the update tojoccurs. It is possible, however, that any given invocation of methodtwomight observe a value forjthat is much greater than the value observed fori, because methodonemight be executed many times between the moment when methodtwofetches the value ofiand the moment when method two fetches the value ofj.See §17.4 for more discussion and examples.
Labels:
Java
28 Mar 2012
Just a tips with grep
It searches recursively, ignores binary files, and doesn't look inside Subversion hidden folders.(...)
grep -Ir --exclude="*\.svn*" "pattern" *
24 Mar 2012
Daemon Thread and User Thread
In java we have two type of Threads : Daemon Thread and User Threads. Generally all threads created by programmer are user thread (unless you specify it to be daemon or your parent thread is a daemon thread). User thread are generally meant to run our programm code. JVM doesn't terminates unless all the user thread terminate.
On the other hand we have Daemon threads. Typically these threads are service provider threads. They should not be used to run your program code but some system code. These thread run parallelly to your code but survive on the mercy of the JVM. When JVM finds no user threads it stops and all daemon thread terminate instantly. Thus one should never rely on daemon code to perform any program code.
For better understanding consider a well known example of Daemon thread : Java garbage collector. Garbage collector runs as a daemon thread to reclaim any unused memory. When all user threads terminates, JVM may stop and garbage collector also terminates instantly.
On the other hand we have Daemon threads. Typically these threads are service provider threads. They should not be used to run your program code but some system code. These thread run parallelly to your code but survive on the mercy of the JVM. When JVM finds no user threads it stops and all daemon thread terminate instantly. Thus one should never rely on daemon code to perform any program code.
For better understanding consider a well known example of Daemon thread : Java garbage collector. Garbage collector runs as a daemon thread to reclaim any unused memory. When all user threads terminates, JVM may stop and garbage collector also terminates instantly.
Labels:
Java
28 Feb 2012
Some tips for Mac users
Show all hidden files:
Unlock folder recursively:
defaults write com.apple.finder AppleShowAllFiles TRUE killall Finder
Unlock folder recursively:
sudo chflags -R nouchg * ./
Subscribe to:
Posts (Atom)