Java Threads in 60 Seconds

  • java.lang.Thread class is used to create threads in Java. All code in Java executes in the context of some thread.
  • Threads could be created by overriding the `run()` method in the Thread class or by passing a Runnable instance to its constructor.
  • Runnable defines a task that is to be performed by a thread.
  • To start a thread, call the Thread.start() method.
  • To block until a thread is finished, call the blocking Thread.join() method.
  • To send a stop signal to a thread, use Thread.interrupt() method. There is no guarantee that the thread will stop. Well-behaved threads act on it by stopping or taking another action. If the thread is blocked on blocking method that supports interruption, InterruptedException is thrown. (ServerSocket.accept() blocking method doesn’t respond to interruption).
  • Do not use stop(), suspend() or destroy(). These methods have been deprecated.
  • wait/notify mechanism is used when a thread needs to send a signal to another that a condition has been met (an alternative to poll-sleep-poll loop to check on a condition). The thread calling Object.wait() on an object blocks until another thread calls Object.notify() (or notifyAll())on the same object.



One thought on “Java Threads in 60 Seconds

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s