출처 | https://www.inflearn.com/course/%EC%A6%90%EA%B1%B0%EC%9A%B4-%EC%9E%90%EB%B0%94
암달의 법칙은 암달의 저주로도 불리며 컴퓨터 시스템의 일부를 개선할 때 전체적으로 얼마만큼의 최대 성능 향상이 있는지 계산하는 데 사용된다. 진 암달의 이름에서 따왔다. 이론만 많은 컴퓨터과학 분야에서 몇 안 되는 법칙이다.
package Thread;
public class MyThread extends Thread{
//1. Thread클래스를 상속받는다.
//2, run() 메소드를 오버라이딩한다.
//3. 동시에 실행시키고 싶은 코드를 선택한다.
private String str;
public MyThread(String str)
{
this.str = str;
}
@Override
public void run() {
for( int i = 0; i < 10; i++)
{
System.out.println("+");
try{
Thread.sleep(1000);
}catch (InterruptedException e)
{
e.printStackTrace();
}
}
}
}
package Thread;
public class MyThreadExam {
public static void main(String[] args) {
String name = Thread.currentThread().getName();
System.out.println("Thread name : " + name);
System.out.println("Start");
MyThread mt1 = new MyThread("*");
MyThread mt2 = new MyThread("+");
mt1.start();
mt2.start();
System.out.println("end!!!");
}}
프로그램은 모든 스레드가 종료되야 종료된다.
package Thread;
public class MyThreadExam2 {
public static void main(String[] args) {
String name = Thread.currentThread().getName();
System.out.println("Thread name : " + name);
System.out.println("Start");
MyRunnable mr1 = new MyRunnable("*");
MyRunnable mr2 = new MyRunnable("+");
// Thread 인스턴스를 생성하는데, 생성자에 Runnable 인스턴스를 넣어준다.
Thread t1 = new Thread(mr1);
Thread t2 = new Thread(mr2);
t1.start();
t2.start();
System.out.println("end!!!");
}}
package Thread;
//Runnable 인터페이스를 구현한다.
public class MyRunnable implements Runnable{
private String str;
public MyRunnable(String str)
{
this.str = str;
}
// run() 메소드를 오버라이딩한다.
@Override
public void run() {
for( int i = 0; i < 10; i++)
{
System.out.println("+");
try{
Thread.sleep(1000);
}catch (InterruptedException e)
{
e.printStackTrace();
}
}
}
}