You are on page 1of 5

Package java.

util
Contains the collections framework, legacy collection classes, event model, date and time facilities, internationalization, and miscellaneous utility classes (a string tokenizer, a random-number generator, and a bit array).

Interface Runnable
All Known Implementing Classes: AsyncBoxView.ChildState, RenderableImageProducer, Thread, TimerTask public interface Runnable

The Runnable interface should be implemented by any class whose instances are intended to be executed by a thread. The class must define a method of no arguments called run.

This interface is designed to provide a common protocol for objects that wish to execute code while they are active. For example, Runnable is implemented by class Thread. Being active simply means that a thread has been started and has not yet been stopped.

In addition, Runnable provides the means for a class to be active while not subclassing Thread. A class that implements Runnable can run without subclassing

Thread by instantiating a Thread instance and passing itself in as the target. In most cases, the Runnable interface should be used if you are only planning to override the run() method and no other Thread methods. This is important because classes should not be subclassed unless the programmer intends on modifying or enhancing the fundamental behavior of the class.

public class Thread


extends Object implements Runnable

A thread is a thread of execution in a program. The Java Virtual Machine allows an application to have multiple threads of execution running concurrently.

Every thread has a priority. Threads with higher priority are executed in preference to threads with lower priority. Each thread may or may not also be marked as a daemon. When code running in some thread creates a new Thread object, the new thread has its priority initially set equal to the priority of the creating thread, and is a daemon thread if and only if the creating thread is a daemon.

When a Java Virtual Machine starts up, there is usually a single non-daemon thread (which typically calls the method named main of some designated class). The Java Virtual Machine continues to execute threads until either of the following occurs:

The exit method of class Runtime has been called and the security manager has permitted the exit operation to take place. All threads that are not daemon threads have died, either by returning from the call to the run method or by throwing an exception that propagates beyond the run method.

There are two ways to create a new thread of execution. One is to declare a class to be a subclass of Thread. This subclass should override the run method of class Thread. An instance of the subclass can then be allocated and started.

The Java language includes three important methods that effectively allow one thread to signal to another. Without this facility, various constructs used in concurrent programming would be difficult and inefficient to implement, at least prior to Java 5 (see below).

'wait and notify':


We can call the wait() method of any Java object, which suspends the current thread. The thread is said to be "waiting on" the given object. Another thread calls the notify() method of the same Java object. This "wakes up" one of the threads waiting on that object.

In some of the following discussion, we'll use the term wait/notify mechanism, and refer to the two methods wait() and notify(). However, we'll see that much of the discussion includes notifyAll() and we'll look at when to use the two variants below.

Why do we need Synchronization in Java?


If your code is executing in multi-threaded environment you need synchronization for objects which are shared among multiple threads to avoid any corruption of state or any kind of unexpected behavior. Synchronization in Java will only be needed if shared object is mutable. if your shared object is read only or immutable object you don't need synchronization despite running multiple threads. Same is true with what threads are doing with object if all the threads are only reading value then you don't require synchronization in java. JVM guarantees that Java synchronized code will only be executed by one thread at a time.

In Summary Java Synchronized Keyword provides following functionality essential for concurrent programming :

1) synchronized keyword in java provides locking which ensures mutual exclusive access of shared resource and prevent data race. 2) synchronized keyword also prevent reordering of code statement by compiler which can cause subtle concurrent issue if we don't use synchronized or volatile keyword. 3) synchronized keyword involve locking and unlocking. before entering into synchronized method or block thread needs to acquire the lock at this point it reads data from main memory than cache and when it release the lock it flushes write operation into main memory which eliminates memory inconsistency errors.

how to draw clock hands?


i can display a digital clock using the Date class but i cant seem to figure out how to draw the analog clocks hour, minute and second hands. the center of the clock is (330, 280) and i've tried a few math calculation but cant seem to get it right. i know that the co-ordinate of the other point uses a math calculation eg. x2 = radius * cos theta

You might also like