Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 02-05-2009
Member
 
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.
Reply With Quote
  #2  
Old 02-05-2009
Member
 
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.
Reply With Quote
  #3  
Old 02-05-2009
Member
 
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();
    }
}
Reply With Quote
  #4  
Old 02-05-2009
Janupagla
 
Posts: n/a
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.
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "How to achieve Multithreading in Java"
Thread Thread Starter Forum Replies Last Post
What is the difference between multithreading and multiprocessing? Alphonsus Software Development 8 27-04-2011 11:23 PM
Multithreading in intel IPP Assasin boy Motherboard Processor & RAM 9 26-09-2010 03:25 AM
Multithreading and ArrayList Logan 2 Software Development 5 09-02-2010 05:16 AM
run method of MultiThreading beelow Software Development 3 14-11-2009 10:18 AM
forks and multithreading invincibledj20 Software Development 0 12-11-2008 07:55 PM


All times are GMT +5.5. The time now is 01:38 AM.