Why we need multithreading in java I am going to explain in most simple easy way.

Imagine google is giving you data in seconds literally 1000’s of links are fetched in less than a second.How this damn thing is possible.Let us take one example if you and your cousin is at home you are told to make the lunch at home and clean the stuff.So what we do in this case we just divide the task some one will cook and the other person will clean the home as two people are doing the task simultaneously it is going to take less time.

Same thing when you want to perform some job with computer.

Such tasks are done in java using threads.Threads are the worker thread that can operate on a job.

How we create the threads?

     1.Extend the Thread class.

     2.Implement the Runnable interface.



Let us learn first method of creating the Thread.

 

1.Extend the Thread class


Now Few things to clarify


What extends Thread provides to us?

     It gives us ability to create our own thread.

But you may ask what is the use of thread where should i specify what task it should do?

     To specify what it should you should write it in method

 

What is Thread.sleep(100)?

     this method says hey currently executing thread just wait for 100 miliseconds.after that you can execute.

What is this.getName()?

     Suppose there are many threads running in parellal how can i know which thread is running and doing what

that’s why we call getName method on currently executing thread to know it’s name.

How can set name to my newly created thread?

     threadObj.setName(“setNameHere”);

setName needs to be called upon the thread’s object.

 


What if my thread has some urgent task to do and it should execute before other thread? Can i declare somewhere such thing?

Yes. you can do it.Priority can range from the 1 to 10 where 5 is normal priority.

table.setPriority(6);

What if i have one thread which needs to be executed right after execution of the other thread?

call join method on the thread.it tells currently executing thread that you should wait because other thread needs to be executed first.

 

Now let us understand the second way of creating the threads.

  • Implement Runnable interface

Steps

1.create class that implements runnable.



2.Create instance of that class 


              DoTask doObj=new DoTask();


3.Pass this instance to instance of thread class.

              Thread threadObj=new Thread(doObj);


4.call thread’s start() 


              threadObj.start();


See the complete Code.

 


 


Points to study

Synchronisation between threads.

In our day to day life when someone is using the phone you cannot access it until the phone is handed over to you.

Same thing happens with threads suppose one thread accessing the object which contains list of employees.

  • One thread adds entries to that ArrayList when first thread is done.
  • Second thread will access the list.
  • Once second thread completes then third thread will access.

 


wait and notify,notifyall methods

  1. Wait method is used for the purpose of the pausing the thread 
  2. Notify method is used for making an announcement to other thread that I am done with my work you can now access the resource I am releasing it.
  3. notifyall() informs all available threads that i am releasing the resource you can use it.

Leave a Reply

Your email address will not be published. Required fields are marked *