Sunday, December 8, 2013

multiple threads

1. Basic info about thread:
A thread is a thread of execution in a program. The Java Virtual Machine allows an application to have multiple threads of execution running concurrently.

When code running in some thread creates a new Thread object, the new thread has its priority initially set equal to the priority of the creating thread.




  • All Java threads have a priority and the thread with he highest priority is scheduled to run by the JVM.
  • In case two threads have the same priority a FIFO ordering is followed.


  • What I try to say is about word concurrently. When there are multiple threads running concurrently, but at any given single moment, there is only one single thread running. If the threads all have the same priority, then JVM will take turns to run each of them in a super fast fashion, which tricks human being to think they are all running on the same time. In fact, it's like each of them runs for a super short time, then pauses for a few super short times, and then runs again for a super short time, then pauses for a few super short times, and so on.

    2. Basic information about synchronized keyword:
    The synchronized keyword may be applied to a method or statement block and provides protection for critical sections that should only be executed by one thread at a time, which is the one I highlighted above as "any given single moment. 

    Basically what synchronized means is simply to say "I lock something and only I can modify it, once I'm done with it then it's unlocked and somebody else can modify it. "

    Here is a piece of code that has two threads to increment the information 5000 times. Basically one thread locks the information and increments it 5000 times so it increments it from 1 to 5000 in a try,after it's done it releases the information and another thread does the same.
    Without using synchronized keyword to lock the information, two threads will take turn to increment the information, which is not what I want.

    • When applied to a static method, the entire class is locked while the method is being executed by one thread.
    • When applied to an instance method, the instance is locked while being accessed by one thread
    • When applied to an object or array, the object or array is locked while the associated code block is executed by one thread

    Here goes the code:
    public abstract class Multithread {
        private static Integer info = new Integer(0);
        static Thread t1 = new Thread() {
            public void run() {
                synchronized (info) {
                    for (int i = 0; i <= 5000; ++i) {
                        System.out.println("world " + info++ + " "
                                + this.getPriority());
                    }
                }
            }
        };
        static Runnable t2 = new Runnable() {
            public void run() {
                synchronized (info) {
                    for (int i = 0; i <= 5000; ++i) {
                        System.out.println("hello " + info++ + "  "
                                + Thread.currentThread().getPriority());
                    }
                }
            }
        };

        public static void main(String[] args) throws InterruptedException {
            t1.start();
            new Thread(t2).start();
        }
    }


    Output will be like: 
    world 1       5
    ....
    world 4996 5
    world 4997 5
    world 4998 5
    world 4999 5
    world 5000 5       // thread 1 is done with the information and release it.
    hello 5001  5
            // thread 2 picks up the information and increments it.
    hello 5002  5
    ...
    hello 10001 5

    No comments:

    Post a Comment