File tree 1 file changed +41
-0
lines changed
1 file changed +41
-0
lines changed Original file line number Diff line number Diff line change 3
3
* Thread can be created using ** Runnable Interface** or ** Extending Thread**
4
4
* Thread.sleep(mills) pauses the current thread execution for defined ** millseconds** .
5
5
* We can also use Thread.sleep(mills,nano) which indicates the time in millisecond and nanosecond.
6
+
7
+ ** ThreadDemo.java**
8
+ ``` java
9
+ class ThreadDemo extends Thread {
10
+ String name;
11
+ ThreadDemo (String n ){
12
+ name = n;
13
+ System . out. println(" Creating: " + name);
14
+ }
15
+
16
+ public void run (){
17
+ System . out. println(" Running: " + name);
18
+
19
+ try {
20
+ for (int i = 4 ; i> 0 ; i-- ){
21
+ System . out. println(" Thread: " + name+ " Printing: " + i);
22
+ Thread . sleep(500 );
23
+ }
24
+ }
25
+ catch (InterruptedException e)
26
+ {
27
+ System . out. println(" Thread " + name + " interupted!" );
28
+ }
29
+ System . out. println(" Thread " + name+ " exiting" );
30
+ }
31
+
32
+ }
33
+ ```
34
+
35
+ ** RunThread.java**
36
+ ``` java
37
+ class RunThread {
38
+ public static void main (String [] args ) {
39
+ ThreadDemo x = new ThreadDemo (" Thread 1" );
40
+ x. start();
41
+
42
+ ThreadDemo y = new ThreadDemo (" Thread 2" );
43
+ y. start();
44
+ }
45
+ }
46
+ ```
You can’t perform that action at this time.
0 commit comments