Thread
Thread?
MutiThreadingの利点
Thread in Java
1.JavaでのThreadの使用
Thread 클래스 상속
Runnable 인터페이스 구현
Lamda 표현식으로 Runnable 인터페이스 구현
→public void run()メソッドoverride public class ThreadExample1{
public static void main(String[] args) {
// 1. Thread 클래스 상속
MyThread1 myThread = new MyThread1();
myThread.start();
System.out.println("Hello, My Child!");
// 2. Runnable 인터페이스 구현
Thread thread = new Thread(new MyThread2());
thread.start();
System.out.println("Hello, My Runnable Child!");
// 3. Lambda로 Runnable 인터페이스 구현
Runnable task = () -> {
try {
while (true) {
System.out.println("Hello, Lambda Runnable!");
Thread.sleep(500);
}
} catch (InterruptedException ie) {
System.out.println("I'm interrupted");
}
};
System.out.println("Hello, My Lambda Child!");
}
}
class MyThread1 extends Thread{
public void run() {
try {
while (true) {
System.out.println("Hello, Thread!");
Thread.sleep(500);
}
} catch (InterruptedException ie) {
System.out.println("I'm interrupted");
}
}
}
class MyThread2 implements Runnable {
@Override
public void run() {
try {
while (true) {
System.out.println("Hello, Runnable!");
Thread.sleep(500);
}
} catch (InterruptedException ie) {
System.out.println("I'm interrupted");
}
}
}
OUTPUT(継承1.Threadクラス)
Hello, My Child!
Hello, Thread!
Hello, Thread!
Hello, Thread!
Hello, Thread!
Hello, Thread!
Hello, Thread!
勘定科目勘定科目main thread作成MyThread 1→start()→run():start()ポイントではcontextx-switchingは発生していないため、mainthreadの
Hello, My Child!
が最初に出力されます.その後、MyThread 1のHello, Thread!
をcontextx-switchingで出力する.2.親スレッド待ち:join
public class ThreadExample2 {
public static void main(String[] args) {
Runnable task = () -> {
for (int i = 0; i < 5; i++) {
System.out.println("Hello, Lambda Runnable!");
}
};
Thread thread = new Thread(task);
thread.start();
try {
thread.join(); // 이때 돌고 있는건 main
} catch (InterruptedException ie) {
System.out.println("Parent thread is interrupted");
}
System.out.println("Hello, My Joined Child!");
}
}
OUTPUT
Hello, Lambda Runnable!
Hello, Lambda Runnable!
Hello, Lambda Runnable!
Hello, Lambda Runnable!
Hello, Lambda Runnable!
Hello, My Joined Child!
勘定科目勘定科目main thread作成MyThread 1→start()→run()→join():join()で回転しているメインスレッドが待機状態に入ります.start()のtask threadがすべて実行されるとmainthreadの
Hello, My Joined Child!
が出力されます.3.スレッドのタイプ:interrupt
public class ThreadExample3 {
public static void main(String[] args) throws InterruptedException {
Runnable task = () -> {
try {
while (true) {
System.out.println("Hello, Lambda Runnable!");
Thread.sleep(100);
}
} catch (InterruptedException ie) {
System.out.println("I'm interrupted");
}
};
Thread thread = new Thread(task);
thread.start();
Thread.sleep(500);
thread.interrupt();
System.out.println("Hello, My Interrupted Child!");
}
}
OUTPUT
Hello, Lambda Runnable!
Hello, Lambda Runnable!
Hello, Lambda Runnable!
Hello, Lambda Runnable!
Hello, Lambda Runnable!
I'm interrupted
Hello, My Interrupted Child!
Multithreading in a Muiticore system
*interleapping:埋め込み後処理.
多目的注意事項
並列処理方法
Amdahl's Law
https://www.inflearn.com/course/%EC%9A%B4%EC%98%81%EC%B2%B4%EC%A0%9C-%EA%B3%B5%EB%A3%A1%EC%B1%85-%EC%A0%84%EA%B3%B5%EA%B0%95%EC%9D%98/dashboard
Reference
この問題について(Thread), 我々は、より多くの情報をここで見つけました https://velog.io/@agpine12/Threadテキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol