Results 1 to 4 of 4

Thread: How to achieve Multithreading in Java

  1. #1
    Join Date
    Apr 2009
    Posts
    37

    How to achieve Multithreading in Java

    Hello,

    I know java is very rich language as well as object oriented, and keeping this in mind i started developing multithreading environment in my recent application but need to have proper guidance, so can anyone guide me how to write an application for multithreading, example for understanding will also help.

    Thanks in Advance.

  2. #2
    Join Date
    Mar 2008
    Posts
    232

    Re: How to achieve Multithreading in Java

    Multithreading in JAVA has vital role to play and its has two main important reasons to do so.
    First, multithreading enables user to write very efficient and reliable programs because it lets user to utilize the idle time that is present in most programs.

    The second reason that multithreading is important to Java is to have eventhandling model. Under this program (lets consider about an applet) must respond quickly to an event and then return.

    Java’s multithreading system is built upon the Thread class and its companion interface, which is Runnable. Under the multithreading thread encapsulates a thread of execution. To create a new thread, program will either implement the Runnable interface or extend Thread. Both Runnable and Thread are packaged in java.lang. Thus, they are automatically available to all programs.

  3. #3
    Join Date
    Mar 2008
    Posts
    258

    Re: How to achieve Multithreading in Java

    Here I will provide you an programming example which helps you understand how it works.

    Code:
    import java.lang.*;
    import java.applet.Applet;
    
    public class MultithreadingExample extends Applet
    {
      Example EXA;
    
      public void start()
        {
          int i;
    
          CDE=new Example();
          for(i=0;i<20;i++)  /* start 20 threads */
            new Thread(EXA,new String("booThread"+i)).start();
        }
    
      public void stop()
        {
          EXA.stopThreads();
        }
    }
    
    class MultithreadingExample extends Object implements Runnable
    {
      int num1=0;
    
      public void run()
        {
          int i=0;
          for(;;)
            {
              for(i=0;i<1000;i++)
                {
                  num=num+10;
                  num=num-10;
                }
    
              try {Thread.sleep(10000);}
              catch (InterruptedException e ) {}
              System.out.println(Thread.currentThread().getName()+
                         " sees the number: " + num1);
            }
        }
    
      void stopThreads()
        {
          Thread tArray[];
          int numThreads;
    
          numThreads=Thread.activeCount();
          tArray=new Thread[numThreads];
          Thread.enumerate(tArray);
          for(int i=0;i<numThreads;i++)
          if(tArray[i].getName().startsWith("booThread"))
            tArray[i].stop();
        }
    }

  4. #4
    Janupagla Guest

    Re: How to achieve Multithreading in Java

    A program or process can contain multiple threads that execute instructions according to program code. Like multiple processes that can run on one computer, multiple threads appear to be doing their work in parallel. Implemented on a multi-processor machine, they actually can work in parallel. Unlike processes, threads share the same address space; that is, they can read and write the same variables and data structures.

    When you're writing multithreaded programs, you must take great care that no one thread disturbs the work of any other thread. You can liken this approach to an office where the workers function independently and in parallel except when they need to use shared office resources or communicate with one another. One worker can speak to another worker only if the other worker is "listening" and they both speak the same language. Additionally, a worker can't use a copy machine until it is free and in a useable state (no half-completed copy jobs, paper jams, and so on). As we work through this article, you'll see how you can get threads to coordinate and cooperate in a Java program much like workers in a well-behaved organization.

    In a multithreaded program, threads are obtained from the pool of available ready-to-run threads and run on the available system CPUs. The OS can move threads from the processor to either a ready or blocking queue, in which case the thread is said to have "yielded" the processor. Alternatively, the Java virtual machine (JVM) can manage thread movement -- under either a cooperative or preemptive model -- from a ready queue onto the processor, where the thread can begin executing its program code.

    Cooperative threading allows the threads to decide when they should give up the processor to other waiting threads. The application developer determines exactly when threads will yield to other threads, allowing them to work very efficiently with one another. A disadvantage is that a malicious or poorly written thread can starve other threads while it consumes all available CPU time.

    Under the preemptive threading model, the OS interrupts threads at any time, usually after allowing them to run for a period of time (known as a time-slice). As a result, no thread can ever unfairly hog the processor. However, interrupting threads at any time poses problems for the program developer. Using our office example, consider what would happen if a worker preempts another worker making copies halfway through her copy job: the new worker would start his copy job on a machine that already has originals on the glass or copies in the output tray. The preemptive threading model requires that threads use shared resources appropriately, while the cooperative model requires threads to share execution time. Because the JVM specification does not mandate a particular threading model, Java developers must write programs for both models. We'll see how to design programs for either model after looking a bit at threads and communication among threads.

Similar Threads

  1. What is the difference between multithreading and multiprocessing?
    By Alphonsus in forum Software Development
    Replies: 8
    Last Post: 27-04-2011, 11:23 PM
  2. Multithreading in intel IPP
    By Assasin boy in forum Motherboard Processor & RAM
    Replies: 9
    Last Post: 26-09-2010, 03:25 AM
  3. Multithreading and ArrayList
    By Logan 2 in forum Software Development
    Replies: 5
    Last Post: 09-02-2010, 05:16 AM
  4. run method of MultiThreading
    By beelow in forum Software Development
    Replies: 3
    Last Post: 14-11-2009, 10:18 AM
  5. forks and multithreading
    By invincibledj20 in forum Software Development
    Replies: 0
    Last Post: 12-11-2008, 07:55 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,714,017,612.48614 seconds with 17 queries