-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMultiThreadingEx.java
More file actions
53 lines (53 loc) · 1.12 KB
/
Copy pathMultiThreadingEx.java
File metadata and controls
53 lines (53 loc) · 1.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
//MultiTasking = Doing multiple tasks at a time
//MultiThreading = doing many sub processes for one task at a time
//Using of Thread two ways :-
/*1.by extending the Thread & 2. By implementing the Runnable
* if use runnable we create the object of a Thread
*/
// By extending the Thread
class Hai extends Thread // to access Thread functions
{
public void run() // runnable method
{
for(int i=1;i<=5;i++)
{
System.out.println("Hai");
try
{
Thread.sleep(1000); //in milliseconds
}
catch(Exception e)
{
System.out.println("error");
}
}
}
}
class Hello extends Thread // to access Thread functions
{
public void run() //runnable method
{
for(int i=1;i<=5;i++)
{
System.out.println("Hello");
try
{
Thread.sleep(1000);//need to write in exception handling method
}
catch(Exception e)
{
System.out.println("error");
}
}
}
}
public class MultiThreadingEx
{
public static void main(String[] args)
{
Hai obj1 = new Hai();
Hello obj2 = new Hello();
obj1.start(); //starts the obj1 methods execution
obj2.start(); //starts the obj2 methods execution
}
}