forked from techpanja/interviewproblems
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThreadExample.java
More file actions
36 lines (32 loc) · 1.14 KB
/
ThreadExample.java
File metadata and controls
36 lines (32 loc) · 1.14 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
package threads.main;
/**
* Created with IntelliJ IDEA.
* User: rpanjrath
* Date: 8/29/13
* Time: 2:13 PM
* To change this template use File | Settings | File Templates.
*/
public class ThreadExample {
public static void main(String[] args){
// System.out.println(Thread.currentThread().getName());
// for(int i=0; i<10; i++){
// new Thread("" + i){
// public void run(){
// System.out.println("Thread: " + getName() + " running");
// }
// }.start();
// }
Thread myThread = new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
}
System.out.println("inner thread: " + Thread.currentThread().getName());
}
}, "myThread1");
myThread.run(); // calling run will have the original thread i.e. main here execute without spawning a new thread.
myThread.start(); // calling start will have the new thread .. myThread1 ... execute parallely.
}
}