Skip to main content

Posts

Showing posts with the label Runnable

Defining starting thread and Thread vs Runnable

Java threads are objects like any other Java objects. Threads are instances of class  java.lang.Thread , or instances of subclasses of this class. In addition to being objects, java threads can also execute code. Creating and Starting Threads Here is the code to create a Thread and start its execution. 1 2 3 4 5 // Creates Thread Object Thread thread = new Thread();    // Starts Thread execution thread.start(); This example doesn't specify any code for the thread to execute. The thread will stop right away after it is started. There are two ways to specify what code the thread should execute. To create a subclass of Thread and override the  run()  method. To implement a  Runnable ( java.lang.Runnable ) interface, override its   run()  method and pass that Runnable  object to the  Thread  constructor. Thread Sub...