Thread states, brief intro about synchronization of threads and thread class as well as Runnable interface

ok lets continue with multithreading now we have seen the difference between the thread and the process and the internal structure about the threads and process now lets go deeper in it about the thread states..
  •  For the threads to be in execution it undergoes with several states they are 
  1. New
  2. Ready
  3. Running
  4. Waiting
From running it can go to stop or block and from stop and block and from sop and block it can go to ready state lets discuss  all the states first .

Initially
  • New means A thread that has not yet started is in this state.
  • RUNNABLE. A thread executing in the Java virtual machine is in this state.
  • BLOCKED. A thread that is blocked waiting for a monitor lock is in this state.
  • suspended.A thread is suspended momentarily but it again runs from where it left.
  • Terminated .A thread when we ment to immediate halt but cannot be resumed from where it left


these all were the thread states.....

now lets talk regarding the thread priorities as said a little in the previous post now will clarify here more deeply
  • java asigns each thread a priority that determines how thread should be treated with respect to the others.
  • priorities are nothing but the integers that specify the relative priority of one thread with the others. As an absolute value its meaningless that is its not higher priority thread runs faster then the lower one.
  • Instead it is used to decide which thread to run next.
  • this concept is called context switching.
Its necessary to know when the context switch takes place in which circumstances
  1. A thread can voluantary relinquish control that means it left  willingly that its work is over and now want to leave in that way.
This can happen explicitly yielding sleeping blocking or for an i/o which is pending.
    
     2.    A thread can be preempted by a higher -priority thread ( preemptive means                   taking control forcefully )a lower priority thread that does not yield the                           processor is simply preemted no matter whatever it is doing by the higher                     priority thread.

                                               synchronization of threads

  •  Now talking about the synchronization of threads first we have to clear the concept about  the critical region as if critical region the term is not known the explanation wouldn't be clear so defining critical region we must say that it is the region where two or more process try to acquire the shared resources so if we have process 1 and process 2 they try to acquire the same shared resource at the same time so this is all about critical region in short terms it is the region where both process try to acquire the shared resource.
  • So here both the process try to acquire the resource at the same time then who to give the resource becomes the question and lets take the example that in bank two person share the same account then one tries to withdraw money at the same time other person also tries to do so say in the bank the total money is 700 and 1st person takes 500 at the same time other also withdraws 500 but if they both process at the same time than it would be loss to the bank as it has 700 and withdrawal was of 1000 .so here by practical example i want to explain that synchronization is necessary so if one is transacting other has a waiting or busy message first then calculates the database and proceeds further for the second client.
  • But what is the option for the solution for this........?? 
  • The solution is the monitor as we studied in the operating system monitor is the main thing in the synchronization we can consider it as a small box which dosent allows no to process to execute concurrently or parallel.
  • so in thread we can consider that once the thread is inside the synchronized method ,no other thread can call any other synchronized method on the same object .
  • As in java every thing is given ready made i mean to say that of each and every thing classes are made and inherited or interface are implemented but here we dont have any class like monitor, instead each object has its own implicit monitor that is automatically entered when one of the objects synchronized method is called .

Now moving in the topic for the thread class and Runnable interface 

  • Java's multithreadig is dependent on the thread class in -built provided by the java and the Runnable interface which is implemented .
  • so to use the several methods which are important for the creation and several other states as our program goes through we have the thread class which is to be extended to our class so we can use that method and for the interface Runnable we have to write implements Runnable .
  • some of the methods which are provided in -built such as .....
  • getName :- here we can obtain the threads name by default it might be main or if we                       have set earlier than it would return that name of the thread for eg                                 first,second,kb etc.
  • getPriority:- here we can get the priority so we can think its return type is intrger as it                         returns the integer value from 1 -10 as the normal priority is 5 but can                       increase as well as decrease but by default it is 5.
  • isAlive :- returns true or false so we can say that is thread is currently available or                     not is thread alive or not if corrupted or destroyed or preempted by other                     thread then that particular thread will return false or if working currently                         then will return true so its return type is boolean .
  • join :- here what this method says is that by convention we think that the main thread              should exit last after the execution of its sub threads so we couldnt guarentee              that at last only the main thread will exit so at first we have sleep method and                still also by sleep method we couldnt guarentee, the other method is the join if              we call the join method by threadname.threadclass object.join ( ) it means                that by executing all these sub- threads and at last ,main thread will be                          executed so the role of join matters in this.
  • run:-   here we can say that it is the starting point of the thread.
  • sleep:-here we can say that it halts or suspends the thread from being executed in                   milliseconds we can give the time till how much time we want to suspend.
  • start:-by this method we can implicitly call the run method but keep in mind run            method is also declared in the interface but we have to define it in our                  class so we cant call when we write implements Runnable as we have to              define it first but in extends thread, the first way it is already defined there



rest of the part will be continued in the next post ....

Comments

Popular posts from this blog

Navigation in Vaadin.

State space search / blocks world problem by heuristic approach /TIC TAC TOE

Drag and drop items from one Grid to another