Pages

21 Dec 2012

[Windows 8] Input method still remains after removing it?

Well, I've upgraded to Windows 8 recently, I tried to install Vietnamese input language method, but then I didn't feel like familiar to it, so I decided to remove it, well, everything was okay at that point.
After that, when I was working, I noticed that there's still Vietnamese input language method when I click on "ENG" button on task bar, that's weird, right? Afterwards, I searched and tried many ways (I even asked on a big forum: http://superuser.com/questions/522696/how-can-i-remove-an-input-method-completely-in-windows-8) to remove it but couldn't help... until this post:

http://answers.microsoft.com/en-us/windows/forum/windows_8-desktop/language-setting-question-display-lang-input-lang/4a057ad5-2965-4257-bc0f-5e1ab1411a63


Summary:
- Just add Vietnamese (or the language which you want to remove), then remove it again!

Hope it can help you out :)


 ---------------------
[UPDATE -20130612]
 ---------------------
 
Oh yeah, as I commented below, the above solution doesn't work, but today, finally, I got rid of it.
Actually, I almost surrendered and I have been having to stand it until today. I was starting learning Japanese, so I added Japanese Input Method to Language Bar and I got 3 entries English, Vietnamese, Japanese. The problem is using Windows+Space to switch between those 3 entries is really annoying, so I had to work my ass off on this problem once more time. And lucky me, I found this thread:

http://answers.microsoft.com/en-us/windows/forum/windows_8-desktop/remove-english-us-keyboard/16f81314-8c4f-4315-baf0-c6518449d8ae

After reading it, I took a look at my startup programs and did some "trial and error", logging in/out several times. Finally, I found out that "Unikey"  (a Vietnamese Input Method software that I was using) is the root cause of the problem, whenever it's started, it adds a Vietnamese entry to Language Bar - that sucks!

Well, I had been using Unikey for years, but at that time, I had to give it up! But then fortunately, after googling, I found another app which can do the same awesome job but DOES'T add anything to Language Bar, and it's:

http://www.trankynam.com/gotv/

I had been using that app on my Android phone, but didn't know it's also available on many other platforms, of course, includes Windows.

So... finally, after a very long time, my problem is solved completely xD

And one more thing, just a thought... that guy, TranKyNam is awesome indeed, he has a few other apps than gotv that I haven't had chances to try, but they looks cool also. I will definitely remember his name xD

20 Nov 2012

Use adb wifi without rooting for Android phones

Well, if your phone is rooted already, it's very easy to use adb without USB cables. Just go to Google Play then search for "adb wifi", you'll see several apps which can help you with your rooted phone.
But if for some reasons, you don't want or mustn't root your phone (because of the warranty or the phone is not yours...), and you hate debugging your app with the cable plugged in your phone, well, there's still a workaround for you (but it still requires to use the USB cable once or twice)

Steps:
  • Connect your Android phone with your  computer by the USB cable, and make sure you have adb and required drivers for you phone installed on your computer. Then type (this command tells adb daemon to listen over Wifi - port 5555):
  • adb tcpip 5555
  • Okay, now you can plug out your USB cable and throw it away (just kidding, you'll might need it in case you restart your phone or you pull out the battery...). Now you have to find your phone IP, go to Setting -> Wi-Fi -> Select the Wi-Fi network your phone is using, then you'll see the IP address. Now on your computer, you have to connect adb to your phone, for example, IP address of my phone is 192.168.0.101, I'll do it by this command:
  • adb connect 192.168.0.101:5555
  • Then you'll see message "connected to 192.168.0.101:5555", now your phone is connected to adb wirelessly. If you want to use adb over USB again, connect your phone to your computer then type in cmd or Terminal:
  • adb usb

That's all, good luck!

17 Nov 2012

A note about .gitignore

First of all, take a look at this page: http://gitready.com/beginner/2009/01/19/ignoring-files.html
And I'm not copying its content lol, well, this is just a note about what I've done successfully with .gitignore to ignore some folders in my project.

Steps:
  • Enter to root directory of your project, then "vi .gitignore" and enter something like:(I suppose you haven't had .gitignore in your project):
  • bin/ # ignore all folders which are named "bin"
    .settings/ # ignore all folders which are named ".settings" 

  • If there's nothing in your "bin" folders as well as ".settings" folders, your job here is done, everything you will put in those folders will not be tracked by Git. But if there's something in those folders which means Git are tracking it already, so you have to let Git know that you don't want to tracking it by these commands:
  • git rm --cached -r Your_Path/bin/
    git rm --cached -r Your_Path/.settings/
    git commit -m "Ignore folder bin and .settings"
    git push origin your_branch
    

P/S: You may doubt about removing cache then commit & push, and yeah, I did, but don't worry. Doing so just removes cache not actual files in your folders (actually I have no idea about where is cache too, but I guess it's cache of git lol), and committing & pushing just delete those folders on remote server, not on your local repository.
In short, making a ".gitignore" just tells your Git program on your computer knows that don't list changes of files in those folders when you type "git status",  and removing cache & committing & pushing just delete those folders on remote repository.

How to install and config Smartgit + Git + Bitbucket.org

  • Download install Git: http://git-scm.com/downloads
  • Download install SmartGit: http://www.syntevo.com/smartgit/download.html
  • Add SSH public key:
    • Generate SSH public key if it hasn't been created
      • Open Git Bash -> Generate SSH public key by typing: ssh-keygen -t rsa -C "your_email@youremail.com" -> Public key will be located in folder "~/.ssh/"
    •  Add SSH public key to bitbucket.org
      • Open bitbucket.org -> Account setting -> SSH keys -> Add Key -> Add content of "~/.ssh/id_rsa.pub" as a key
  • In order to be able to push, you have to config your name and email, open Git Bash, type:
    • git config --global user.email "abcd@xyzt.com"
    • git config --global user.name "Blah Bloh"
  • Clone a project (choose SSH, don't choose HTTPS, otherwise you will not be able to push properly by SmartGit - Actually, I don't know why, it took me a few hours to realize that, but I still can't explain why now lol):
    • git clone git@bitbucket.org:user_name/project_name.git
  • Add project to Smartgit:
    • Project -> Open Repository -> Choose your project folder -> Next...

6 Nov 2012

Synchronized keyword in Java

2 effects:
  • First, it is not possible for two invocations of synchronized methods on the same object to interleave. When one thread is executing a synchronized method for an object, all other threads that invoke synchronized methods for the same object block (suspend execution) until the first thread is done with the object.
  • Second, when a synchronized method exits, it automatically establishes a happens-before relationship with any subsequent invocation of a synchronized method for the same object. This guarantees that changes to the state of the object are visible to all threads.

Explanation about lock object of synchronized block:
  • If you synchonize on the method ( as you're doing by typing public synchronized void addA() ) you lock the whole object, so two thread accessing a different variable from this same object would block each other anyway.
  • If you want to syncrhonize only a variable at a time , so two thread wont block each other while accessing different variables, you have to add them in a synchronized block, and use the variable as the "lock" of the block. If a and b were object references you would use:
public void addA() {
    synchronized( a ) {
        a++;
    }
}
public void addB() {
    synchronized( b ) {
        b++;
    }
}


References:
- http://docs.oracle.com/javase/tutorial/essential/concurrency/syncmeth.html
- http://stackoverflow.com/questions/3369287/synchronized-block-lock-object-question
- http://stackoverflow.com/questions/3047564/java-synchronized-method-lock-on-object-or-method

12 Jul 2012

4 Jun 2012

Add PATH for MAC or Linux temporarily

PATH=$PATH\:/dir/path

/dir/path is the path you want to add to your $PATH

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:
  • 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:

  1. The thread acquires the lock on the monitor for object this .
  2. The thread memory flushes all its variables, i.e. it has all of its variables effectively read from "main" memory .
  3. 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).
  4. (Any changes to variables would normally now be written out to "main" memory, but for geti3() we have no changes.)
  5. The thread releases the lock on the monitor for object this.
So where 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

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:

8.3.1.4. volatile Fields

The 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, volatile fields, that is more convenient than locking for some purposes.
A field may be declared volatile, in which case the Java Memory Model ensures that all threads see a consistent value for the variable (§17.4).
It is a compile-time error if a final variable is also declared volatile.

Example 8.3.1.4-1. volatile Fields
If, in the following example, one thread repeatedly calls the method one (but no more than Integer.MAX_VALUE times in all), and another thread repeatedly calls the method two:
class Test {
    static int i = 0, j = 0;
    static void one() { i++; j++; }
    static void two() {
        System.out.println("i=" + i + " j=" + j);
    }
}
then method two could occasionally print a value for j that is greater than the value of i, because the example includes no synchronization and, under the rules explained in §17.4, the shared values of i and j might be updated out of order.
One way to prevent this out-or-order behavior would be to declare methods one and two to be synchronized (§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 method one and method two from being executed concurrently, and furthermore guarantees that the shared values of i and j are both updated before method one returns. Therefore method two never observes a value for j greater than that for i; indeed, it always observes the same value for i and j.
Another approach would be to declare i and j to be volatile:
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 method one and method two to be executed concurrently, but guarantees that accesses to the shared values for i and j occur 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 for j is never greater than that for i, because each update to i must be reflected in the shared value for i before the update to j occurs. It is possible, however, that any given invocation of method two might observe a value for j that is much greater than the value observed for i, because method one might be executed many times between the moment when method two fetches the value of i and the moment when method two fetches the value of j.
See §17.4 for more discussion and examples.

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.

28 Feb 2012

Some tips for Mac users

Show all hidden files:
defaults write com.apple.finder AppleShowAllFiles TRUE
killall Finder

Unlock folder recursively:
sudo chflags -R nouchg * ./

30 Jan 2012

Free Grep tool for Windows

It's findstr, which is already available in Windows (XP), did you know?

To get more details, you can refer this link: http://technet.microsoft.com/en-us/library/bb490907.aspx, or  type this command in the Command-line:
findstr /?

Here's a typical example to show how to use:
  • If in the Terminal of Unix you type:
grep -R "hi there" \data
  • Then in the Command-line of Windows you can type:
dir /B /S C:\data | findstr /f:/ "hi there"

28 Jan 2012

First post

It's always hard for me to write the first post of a new blog, and this time is not an exception. So to get over it, I will give you some links about me and this blog:
  • Here is the link to about page of this blog
  • And here is the link to my home page where you can find more information about me
Okie, now is 12:52 AM here, it's time to sleep, so bye...