Showing posts with label Thread. Show all posts
Showing posts with label Thread. Show all posts

Thread Related Modifiers in Java

A computer program may have launched more than one process executing concurrently. This is called multithreaded programming. For example you have more than one process in a program executing concurrently, they may attempt to access a class element at the same time. There are a two modifiers that relate to such a situation.

volatile Modifier

The volatile modifier only applies to instance variables. The variables declared volatile are subject to asynchronous modifications. In other words, declaring a variable volatile informs the compiler that this variable may be changed unexpectedly by other parts of the program. So, the compiler takes some special precautions to keep this variable properly updated. The volatile variables are generally used in multithreaded or multiprocessor environments. The volatile modifier tells the accessing thread that it should synchronize its private copy of the variable with the master copy in the memory.



synchronized Modifier

The synchronized modifier is used in multithreaded programming to control access to critical sections in the program. It is used for thread safety in java multi-threaded programming.


Reference(s): SCJP Exam for J2SE 5  

Thread Scheduling in Java

In Java you can use methods to help a thread get into the runnable state, but you cannot directly put the thread into the running state. A thread in the runnable state is eventually put into the running state by the scheduler. The scheduler follows some kind of algorithm to determine which thread should go into the running state. 

There are two main categories of scheduling algorithms:

Preemptive scheduling: 

In this type of scheduling a thread is put into the running state according to the priority. While a thread is running, it can be moved out of the running state only by one of the following conditions:
  • It calls one of the blocking I/O methods.
  • A higher-priority thread gets into the runnable state.


Time sharing: 

In this kind of scheduling algorithm, a thread is allowed to execute only for a limited amount of time. After the time expires, it’s put back into the runnable state, and another thread seeking its turn is put into the running state. Time sharing provides protection against a higher-priority thread using the whole CPU time and keeping all other threads from running. 

Remember that the implementation of the scheduling algorithm in Java is platform dependent. Scheduling is performed to share a resource (the CPU) among multiple threads. Other resources in an application, such as a file, are shared properly by synchronizing the access, as you have already seen. However, sharing the same resource makes threads dependent upon each other, and that dependency may give rise to a deadlock.

If you are curious about deadlock and want to know about it, You may find detail about deadlock here. What is deadlock?


Reference(s): SCJP Exam for J2SE 5

Thread Lifecycle in Java

A thread’s life starts when the method start() is invoked on it. Subsequently, it goes through various states before it finishes its task and is considered dead. Upon calling the start() method, the thread does not start running immediately. The start() method puts it into the runnable state, also called the ready state. It stays in the runnable state until the scheduler puts it into the running state. When the thread goes into the running state, the method run() is called.

Remember that: When the start() method is invoked on a thread, it does not start running immediately; it has to go through a scheduler.

Even during the execution of the thread run() method, the thread may temporarily stop executing and go into one of the nonrunning states, and eventually come back to the running state. The various states of a thread are listed here:

• New: This is the state of a thread when it has been instantiated but not yet started.

• Ready/runnable: A thread enters the runnable state for the first time when the start() method is invoked on the thread instance. Later, the thread can come back to this state from one of the nonrunnable states. In the runnable state, the thread is ready to run and waiting to be selected by the scheduler for running.

• Running: This is the state in which the thread is executing.



Non-runnable states of thread: A thread in the running state may go into one of the three nonrunnable states when it is still alive but not eligible to run. These three states are listed here:
  • Blocked: A thread goes into this state when it’s waiting for a resource such as I/O or anobject’s lock. The availability of the resource will send it back to the runnable state. You will learn about the object’s lock later in this chapter. 
  • Sleeping: This state is one of the timed waiting states because the thread stays in this state for a specific time. A thread goes into this state when its code tells it to sleep for a specific period of time by calling the sleep(…) method. Expiration of the sleep time sends it back to the runnable state.
  • Waiting: The thread goes into the waiting state when the object on which it’s running invokes the wait() method. A call to notify() or notifyAll() (by another thread) will bring it back to the runnable state.
• Dead: A thread is considered dead after the execution of its run() method is complete. A dead thread can never be run as a separate thread again—that is, if you call its start() method, you will receive a runtime exception. However, it is still an object, and you can still call its methods (other than the start() method), but they will be executed in the caller’s thread.

Below figure shows the relationship among all these states of a thread. A running thread may go into blocked or sleeping state and wait there for an event to happen. When the right event happens, the thread goes to the ready state and eventually is put into the running state again by the scheduler.

Different states of a thread


Reference(s): SCJP Exam for J2SE 5 

Making a Thread Safe JSP Page

  • JSP Thread Safe is used to send only one client request for processing 
  • The page directive defines an attribute "isThreadSafe" whose value is either true or false.
    • If the value is set to true which is the default value, the JSP container can send multiple concurrent client requests to the JSP page.
    • If the value of this attribute is set to false, then the JSP container sends client requests only one at a time to the JSP page which makes the JSP page thread safe.
  • Syntax
           <%@ page isThreadSafe="false" %>



If you want to read more about JSP read here