Skip to main content

Posts

Showing posts with the label Thread

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...

Thread in java

Multitasking: Executing several task simultaneously is called multitasking Process-based multitasking :-   Executing several task simultaneously where each task is a separate independent process such type of multitasking is called process based Multitasking.    Example:- While typing a program in the editor we can listen MP3 audio songs. At the same time we download a file from the net.       all these task are executing simultaneously and each task is a separate independent program. hence it is process based multitasking. It is best suitable at operating system level.   Thread-based multitasking:-   Executing several task simultaneously where each task is a separate independent part of the same program is called Thread-based multitasking. and every independent part is called a thread. This type of multitasking is best suitable at programmatic level. Multithreading: Executing several thread simultaneously where each ...