View RSS Feed


Threads in Java. Java multithreading examples.

  1. Using threads in Java applications

    by , 04-03-2012 at 11:57 AM
    Are you going to code a Java application that processes multiple tasks at the same time? Or is your program doing some heavy operations in the background, while the user is viewing a chart report? Such applications are referred as multi-threaded or concurrency programs. The Java platform has built-in support for threading from its very early versions, so developing multi-threaded applications in Java is not tough at all, thanks to the comprehensive and powerful threading and concurrency API. ...
    Tags: java, thread, tutorial Add / Edit Tags
  2. Thread communication

    by , 01-08-2012 at 12:18 PM
    Following methods are used to perform communication between threads.

    • Wait()
    • Notify()
    • notifyAll()

    In the following code, count is a shared variable between different threads. It’s a consumer producer process in which consumer waits in consume() method and waits for producer to perform its action.

    Name:  62.JPG
Views: 506
Size:  26.1 KB

    Thread communication
  3. High-level thread states

    by , 01-08-2012 at 12:16 PM
    Following diagrams display different thread states.

    Name:  62.JPG
Views: 795
Size:  29.7 KB

    Thread States

    • Runnable: Thread in this state is waiting for its turn to start execution.
    • Running: Thread is executing and performing its functionality.
    • Waiting: Thread is in blocked state and waiting for some operation to finish.
    • Sleeping: Thread is suspended forcefully.
    • Blocked on I/O: Thread will start execution after completion of ...
    Tags: states, thread Add / Edit Tags
  4. Thread Creation

    by , 01-08-2012 at 12:12 PM
    Threads can be created using one of the following methods.

    • By Extending Thread class.
    • By Implementing Runnable interface.

    Java Code: Thread creation
    class Counter extends Thread {
    //method where the thread execution will start
    public void run(){
    //logic to execute in a thread
    //let’s see how to start the threads
    public static void main(String[] args){
    Thread t1 = new Counter();
    Thread t2 = new Counter();

    Updated 01-14-2012 at 06:52 PM by Thread

  5. Processes vs Threads

    by , 01-08-2012 at 12:10 PM
    Thread is a single execution in a process as compared to process which is an execution of a program. A process may have multiple threads within it. Also a thread is sometime referred as lightweight process. All the threads in JVM share heap that belongs to the process. In this case, different threads can access same object. Threads always share the heap and contain their own stack to store local variables. In this way threads are kept thread-safe and are synchronized.

    Name:  62.JPG
Views: 3888
Size:  26.7 KB