-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUsingThreads.java
More file actions
32 lines (28 loc) · 1 KB
/
Copy pathUsingThreads.java
File metadata and controls
32 lines (28 loc) · 1 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
// Thread allow aprogram to operate more efficiently by doing multiple things at the same time
// can be used to carry out complex tasks in the background without interrupting the main program
/*
Creating threads:
a. by extending the Thread class and overriding run() method;
b.by implementing the Runnable interface
to avoid concurrency problems its advised to reduce the amount of shred attributes bttwn threads or using the isAlive() method
*/
public class UsingThreads extends Thread {
public void run(){
System.out.println("running a thread");
}
public static void main(String[] args) {
UsingThreads thread = new UsingThreads();
thread.start();
}
}
class Main implements Runnable{
// public static void main(String[] args) {
// Main obj = new Main();
// Thread thread = new Thread(obj);
// thread.start();
// System.out.println("This code is outside of the thread");
// }
public void run(){
System.out.println("running a thread");
}
}