-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMultiThreadingEx1.java
More file actions
55 lines (55 loc) · 1.02 KB
/
Copy pathMultiThreadingEx1.java
File metadata and controls
55 lines (55 loc) · 1.02 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
54
55
//By extending the Thread(printing the values by order)
class Hai1 extends Thread
{
public void run() // runnable method
{
for(int i=1;i<=5;i++)
{
System.out.println("Hai");
try //use the Exception Handling becoz it causes exception
{
Thread.sleep(1000); //in milliseconds
}
catch(Exception e)
{
System.out.println("error");
}
}
}
}
class Hello1 extends Thread // to access Thread functions
{
public void run()
{
for(int i=1;i<=5;i++)
{
System.out.println("Hello");
try
{
Thread.sleep(1000);
}
catch(Exception e)
{
System.out.println("error");//no need to write this line
}
}
}
}
public class MultiThreadingEx1
{
public static void main(String[] args)
{
Hai1 obj1 = new Hai1();
Hello1 obj2 = new Hello1();
obj1.start();
try //just to make to Hai prints first
{ //it makes to print the hai before the hello by 10 msec
Thread.sleep(10);
}
catch(Exception e)
{
System.out.println("error");
}
obj2.start();
}
}