-
Notifications
You must be signed in to change notification settings - Fork 319
/
Copy pathUsingThreads.java
39 lines (32 loc) · 995 Bytes
/
UsingThreads.java
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
package br.com.leonardoz.features.threads;
/**
* Threads
*
* Java support for OS-threads.
*
* Thread is a basic unit that can run in parallel through CPU cores.
*
* A thread can 'see' others threads memory.
*
*/
public class UsingThreads {
public static void main(String[] args) throws InterruptedException {
// Creating
var created = new Thread();
created.start();
// .run() runs on main thread
// Assigning a task for running on a thread - we pass a Runnable instance
var threadWithTask = new Thread(() -> System.out.println("Inside thread" + Thread.currentThread().getName()));
threadWithTask.start();
// Interrupting a thread
Runnable interrupatblyTask = () -> {
while (!Thread.currentThread().isInterrupted()) {
System.out.println("Im not interrupted " + Thread.currentThread().getName());
}
};
var interruptable = new Thread(interrupatblyTask);
interruptable.start();
Thread.sleep(3000);
interruptable.interrupt();
}
}