Multi Threading in Java.

Posted by admin On January - 16 - 2010

It would be nice if we could perform one action at a time and perform it well, but that is usually difficult to do. The human body performs a great variety of operations in parallel—or, as we will say throughout this chapter, concurrently. Respiration, blood circulation, digestion, thinking and walking, for example, can occur concurrently. All the senses—sight, touch, smell, taste and hearing—can be employed at once. Computers, too, can perform operations concurrently. It is common for personal computers to compile a program, send a file to a printer and receive electronic mail messages over a network concurrently. Only computers that have multiple processors can truly execute operations concurrently. Operating systems on single-processor computers use various techniques to simulate concurrency, but on such computers only a single operation can execute at once.

Most programming languages do not enable programmers to specify concurrent activities. Rather, the languages generally provide control statements that only enable programmers to perform one action at a time, proceeding to the next action after the previous one has finished. Historically, concurrency has been implemented with operating system primitives available only to experienced systems programmers.

The Ada programming language, developed by the United States Department of Defense, made concurrency primitives widely available to defense contractors building military command-and-control systems. However, Ada has not been widely used in academia and commercial industry.

Java makes concurrency available to the applications programmer through its APIs. The programmer specifies that applications contain threads of execution, where each thread designates a portion of a program that may execute concurrently with other threads. This capability, called multithreading, gives the Java programmer powerful capabilities not available in the core C and C++ languages on which Java is based.

Here is a program you have to write in java so you can check the working of multi threading.

public class mulThreads extends Thread

{
int count,count1;
String name;
public void run()
{
while(true)
{
name=Thread.currentThread().getName();
if (name.equals(“Marimba”))
System.out.println(“Marimba:” + (++count));
else
System.out.println(“Jini :” + (++count));
try
{
Thread.sleep(500);
}
catch(InterruptedException e ){
}
}
}

public static void main(String args[])
{
mulThreads objmulThreads1 = new mulThreads();
mulThreads objmulThreads2 = new mulThreads();

objmulThreads1.setName(“Marimba”);
objmulThreads2.setName(“Jini”);

objmulThreads1.start();
objmulThreads2.start();
}
}

VN:F [1.9.3_1094]
Rating: 0.0/10 (0 votes cast)
VN:F [1.9.3_1094]
Rating: 0 (from 0 votes)
Share with others:
  • Print
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • StumbleUpon
  • Twitter
  • Twitthis
  • Yahoo! Bookmarks
  • Yahoo! Buzz

Leave a Reply

Help Pakistan's Flood Victims
Ad-Space Available
Our Sponsor

About Me

I will tell you Later

Twitter

    Photos

    Kis galiKabristanDSC09340DSC09302