Skip to content

Commit c346c80

Browse files
committed
Thread Readme Example
1 parent e5433fc commit c346c80

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

Threads/readme.md

+41
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,44 @@
33
* Thread can be created using **Runnable Interface** or **Extending Thread**
44
* Thread.sleep(mills) pauses the current thread execution for defined **millseconds**.
55
* 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+
```

0 commit comments

Comments
 (0)