Java MultiThreading

Home  »    Java   »    Advance Java »  MultiThreading


A Window is used for executing the several programs' simultaneously. This ability is called as he multitasking and Multithreading is a Conceptual programming where a program is divided into the sub programs and these are running parallel. In most of our Computer there is only a Single Processor we know that the speed of input and output doesn't matched with the Processor so a the time of input and output the processor performs the other tasks.

Like in java program first it begins and then it runs his statement and then ends, its execution. It is also called as thread or a single threaded execution because it runs only a single line of execution and a time. Java Support for Running these Single Threaded into the Multiple Lines so far even hey can run at Parallel at a given time. These are also called as tiny programs. But this s important that threads running at the same time but they can share the Execution of a Processor or we can say that they shares the time of CPU, so it appears that they are sunning Concurrently.

So that Multithreading is a powerful tool that makes java different from the other languages and it divides a large programs into a number of threads and execute them in parallel. So that there are only two Application First is called as the Process based Multithreading in which the two Processes are running Concurrently and second is the Thread based Process in which two Process are Running by sharing the time of CPU.



Thread Methods

The Thread Methods are used for Controlling the Behavior of the Executing threads Like Sleeping , Suspend , Sleep all these are Methods, those are Stored Thread Class which is located in the Java. Lang Package. And this class gets Automatically loaded When a Programs begins and we don't have any need to add this Separately. The Thread Class provides followings Methods :-

  1. start() Method

    Start Method is used for Executing a Thread When a new Thread is Created then this will allocate Some Resources of System , S this is the Start Method which will call the Run Method when you start a particular Thread then this will be Automatically Run all the statements those are stored in the Run Method, The syntax is:

  2. yield() Method

    Relinquishes between the threads that are in runnable start and have the same priorities. The yield() method changes the execution of the thread on the basis of round robin or first come first serve scheduling techniques, The syntax is:

  3. run() Method

    Run Method is that which Contains all the Statements those are to be Executed when a Call is made to him. After Creating a Thread when we use Start Method, this will call the Run Method. This Method either can be used by Extending a Thread Class or we can also implement an interface which is known as Runnable, The syntax is:

  4. stop() Method

    The Stop Method , destroy a Thread or Suspend a Running Thread. If we wants to Prevent a Thread from Execution then we can use Stop Method, The syntax is:

  5. sleep() Method

    The Sleep method is used for Suspending the Execution of Current Thread For a Specific Time and For this we have to Specify the time in milliseconds in Sleep Method, The syntax is:

  6. suspend() Method

    This Method Stops the Current Executing Thread until a resume method call is not being made to him, The syntax is:

  7. wait() Method

    Wait Method is used to Stops the Execution of Current Thread and this will put in Wait State until there will not be a resume call made to him, The syntax is:





Thead Life Cycle

The time period between the creation and the destruction of the thread object is called the life cycle of a thread. The five states in thread life cycle are:
  1. Newborn State
  2. Runnable State
  3. Running State
  4. Blocked State
  5. Dead State

A new thread is created in the newborn state. The new thread moves from the newborn state to the Runnable state, when the start() method is invoked. The thread in the Runnable state remains in same state, if the yield() method is invoked. The thread is moved from the Runnable state to suspened state, if the suspend(), sleep() or wait() method is invoked. This suspended thread can come back to Runnable state by again invoking the start() method. The running or suspended thread moves to the dead state, if the stop() method is invoked.



Newborn State

A new thread that is just created is called the newborn thread and the state is known is the newborn state. The thread is not scheduled for running at the newborn state. The newborn thread either calls the start() method to run the thread or calls the stop() method to destroy the thread.



Runnable State

The Thread which has Completed his input and output Operations and now he is waiting for a Processor to Execute the thread is know as the Runnable state and Processor always Available to those threads in the first come and first serve method and in this we use the yield Method which Controls all the threads those have same Priorities.



Running State

A Thread which is Executing is called as Runnable State and we can also say that when a thread is Executed by a Thread then this is also called as Running State and in the running state we can Suspend and resume a Current Running Thread. And at the time of Running we can also use a Sleep Method which will Execute some another thread at a Given amount of Time and when his Time will be finished this will comes back to the First thread.



Blocked State

A thread is called as on blocked state when e is waiting for Processor and a Thread is on the Blocked State when a Thread is at the Suspend state and when he is Slept using Sleep Method and he is waiting for Processor by using Wait Method then at these Stages a thread will be at blocked state.



Dead State

A thread is destroyed Automatically When it has completed his Execution by Executing all the Statements those are Located in the Run Method. And if we want to Destroy a Current running thread , then we can use stop Method.





Main Thread

Every Time When we Make a Program of JAVA then this will make a Main Thread which will makes of initialize the main Program . and in this all the other threads those are called a Child Threads will also Executed. And when a Main thread is called this will first Execute all its child Thread or First its child Threads those are also Called as statements will be Executed and then the Main Thread will be Executed. For displaying the Properties of Current Running Threads ,we use the currentThread Method. This Method will display.

  1. Name of the Thread This will display the Name of Thread and the default is main.
  2. Priority of a Thread Refers to Preference given to a Thread which is 5 by default and which is also called as value of Normal Priority.
  3. Group of the Thread Which Controls the State or Name of Method where he is Currently Executing Statements.


The following example controls the main thread.



The above code will be stored in the file called CurrentThreadDemo.java, which on compilation will crate a class file called CurrentThreadDemo.class. Now compile this file using below commands in the DOS mode:



C:\>jdk1.4\bin>javac CurrentThreadDemo.java
C:\>jdk1.4\bin>java CurrentThreadDemo


"Output of CurrentThreadDemo.class"


Download Complete Program

  -  


Thread Exceptions

When you are going to Create your Thread Programs then you must have to put the Methods in the try block Which will handle all the Exceptions those are made by the Methods of Thread. The Most important Methods of Thread are Run and Sleep those are also must be put into the try and Catch Block.



Working With Multiple Threads

As we know that Multithreading Provides us a ability to Execute the Multiple threads at a same Time But there are also Some Problems when we use Multithreading Suppose There are two Programs those are using same Resource and When they are Running and if first Thread uses a Resource and before Completion of Operations , Second Thread Gets Started then this will create a Problem in using of Resource so that there must be a Proper Control on the Execution of the Threads so that java Provides a Feature in the form of Multithreading is to use Synchronized which defines that when a thread is Executing No Other Thread will be Executed by Processor at that time , Even When a Thread is in the Blocked State and at the slept State then second Thread will not Disturb the Execution of First Thread. For doing this we have to jus put the Synchronize Keyword in front of a Method.



Inter-Thread Communication

As we know that in the Synchronous Communication When a Thread is Executing then no other thread cam disturb the Execution and he will wait, End of Execution of a Single Thread. So that this will waste Time when a second Thread is executing so that we use Inter-based Communication. In this Threads are automatically called by making a Special Call. In the Inter-based Communication we uses only three Methods those are wait, notify and notify all. In this When a Perform Some Action the this will notify the another Thread and when a Second Thread is Performing some Operations then firs thread will be on the wait State and also vice-versa for another's.



MultiThreading


Thread Life Cycle Main Thread
Creating A Thread Thread Priority
Extending The Thread Class Writing Applets With Threads





<< Previous Topic
Next Topic >>



8 comments:

  1. largest companies by market cap. As an investors we have to think not in binary terms but in probabilities and The acceleration of our debt issuance and our government’s seeming indifference to it and to ballooning budget deficits raise the probability and the likely severity of inflation. paypal market cap and facebook market cap

    ReplyDelete
  2. PYTHON TRAINING IN CHENNAI | INFYCLE TECHNOLOGIES:

    Infycle Technologies Chennai’s BEST PYTHON training facility accepts specific module training for any software to improve your skills and shine in your career development. Infycle is the only software training center in Chennai, equipped with more than 150 customized courses to meet the job requirements of candidates, including top positions in multinational companies. Our whole goal is not just to teach theory, but to make you a teacher by providing you with on-site practical training. Therefore, people will be required to process real-time tasks and use cases in real-time, thus bringing you the ultimate encoder. To get all of this, please call 7502633633 for a free demo.
    Best python coursein Chennai

    ReplyDelete
  3. Hi, Thank you for this informative blog, I have just started to learn java course and this blog is really informative for me. Thank you for this blog!

    ReplyDelete