+ 1

What is multithreading in java? Explain with real time example.

8th Feb 2017, 1:57 PM
Arun Yadav
Arun Yadav - avatar
5 Antworten
+ 3
Multithreading is basically executing multiple tasks concurrently via threads. However, due to this ability, there are possible downsides. Race conditions ( when two or more threads race to read or update data ) could occur. Preventing race conditions is simple. Simply by using the synchronized keyword or by using locks ( Lock class available within Java API ). the main idea with Locks is basically each thread which is currently executing that block of code acquires a lock which means no other thread can execute that block of code unless that particular thread releases the lock. There are also downsides of using locks because if an exception occurs before that thread releases it locks then that thread will never release that lock resulting in a similar situation to deadlocks, however, this can be prevented easily by releasing the lock inside a finally block. I can go on forever about the inner workings of threads, however, i think you would be better off finding a video that explains it and try to implement it as that is a faster approach to understanding multithreading.
8th Feb 2017, 2:57 PM
Ousmane Diaw
+ 1
public class PrintThread implements Runnable { private String name ; private int delay ; public PrintThread ( String name, int delay ) { this.name = name ; this.delay = delay ; } public void run ( ) { for ( int i = 0; i < 5; i++ ) { System.out.println ( name + ": " + i ) ; try { Thread.sleep ( delay ) ; } catch ( InterruptedException e ) { e.printStackTrace(); } } } public static void main(String[] args) { Runnable r1 = new PrintThread ( "t1", 200 ); Runnable r2 = new PrintThread ( "t2", 300 ); Thread t1 = new Thread ( r1 ) ; Thread t2 = new Thread ( r2 ) ; t1.start(); t2.start(); } }
8th Feb 2017, 2:37 PM
K.C. Leung
K.C. Leung - avatar
+ 1
Multithreading in Java is a process of executing multiple process simultaneously. Thread is a light-weight process. Multithreading is used to make any game or a product there is need of the multiple process at the same time. Ex:- Watch, there is need of executing multiple process at the same time when the watch all hands (Hour, Minute, Second) is going to cross one another.
8th Feb 2017, 3:10 PM
Arun Yadav
Arun Yadav - avatar
0
public class PrintThread extends Thread { private String name ; private int delay ; public PrintThread ( String name, int delay ) { this.name = name ; this.delay = delay ; } public void run ( ) { for ( int i = 0; i < 5; i++ ) { System.out.println ( name + ": " + i ) ; try { sleep ( delay ) ; } catch ( InterruptedException e ) { e.printStackTrace(); } } } public static void main(String[] args) { Thread t1 = new PrintThread ( "t1", 200 ); Thread t2 = new PrintThread ( "t2", 300 ); t1.start(); t2.start(); } }
8th Feb 2017, 2:30 PM
K.C. Leung
K.C. Leung - avatar
0
By definition, multitasking is when multiple processes share common processing resources such as a CPU. Java is a multi-threaded programming language which means we can develop multi-threaded program using Java. Multi-threading extends the idea of multitasking into applications where you can subdivide specific operations within a single application into individual threads. A multi-threaded program contains two or more parts that can run concurrently and each part can handle a different task at the same time making optimal use CPU. Each of the threads can run in parallel. The OS divides processing time not only among different applications, but also among each thread within an application. https://code.sololearn.com/c2CBv0CeJaF3/?ref=app
23rd Nov 2017, 9:33 AM
Wasula Benjamin
Wasula Benjamin - avatar