class Person {
String name;
public Person(String name) {
this.name = name;
}
public void sayHello() {
for(int i=1; i<=10; i++) {
System.out.println("Hello" + name + "! " + i);
}
}
}
public class PersonTest {
public static void main(String[] args) {
Person p1 = new Person("김유신");
Person p2 = new Person("홍길동");
p1.sayHello();
p2.sayHello();
}
}
<실행결과>
Hello김유신! 1
Hello김유신! 2
Hello김유신! 3
Hello김유신! 4
Hello김유신! 5
Hello김유신! 6
Hello김유신! 7
Hello김유신! 8
Hello김유신! 9
Hello김유신! 10
Hello홍길동! 1
Hello홍길동! 2
Hello홍길동! 3
Hello홍길동! 4
Hello홍길동! 5
Hello홍길동! 6
Hello홍길동! 7
Hello홍길동! 8
Hello홍길동! 9
Hello홍길동! 10
Thread 클래스를 상속받아 공평하게 실행시키고자 하는 일을 run메소드를 오버라이딩하여 그 안에 써준다.
객체를 생성하여 공평하게 일을 시키는 것을 스레드를 가동한다 라고 한다.
start() 메소드를 호출하여 가능하면 공평하게 일을 시킨다.
Runnable 인터페이스를 구현한 클래스를 만들고 run 메소드를 오버라이딩 하여 그 안에 공평하게 시키고자 하는 명령어들을 써준다.
스레드를 가동시키기 위하여 Thread 객체로 포장하여 start()를 호출해야 한다.
class MyBall extends JPanel implements Runnable {
}
setPriority(int newPriority)
우선순위에 설정할 수 있는 값은 1~10
public static final int MAX_PRIORITY 10
public static final int MIN_PRIORITY 1
public static final int NORM_PRIORITY 5
스레드를 가동하면 가능하면 서로 공평하게 실행이 되게끔 스케줄링을 해준다.
반드시 공평한 것은 아니다.
만약, 반드시 공평하게 동작하도록 하려면 스레드 사이에 통신이 필요하다.
상대방 스레드가 종료될 때 까지 내가 "기다리고" 내가 작업이 끝나면 기다리고 있는 상대방 스레드를 "깨워준다."
"기다리고," "깨워주는" 스레드 사이의 통신을 위한 메소드를 이용한다.
스레드 사이의 통신을 위하여 Object의 wait 메소드와 notify 메소드를 이용한다.
//멀티쓰레드의 필요성
class Person {
String name;
public Person(String name) {
this.name = name;
}
public void sayHello() {
for(int i=1; i<=10; i++) {
System.out.println("Hello" + name + "! " + i);
}
}
}
public class PersonTest {
public static void main(String[] args) {
Person p1 = new Person("김유신");
Person p2 = new Person("홍길동");
p1.sayHello();
p2.sayHello();
}
}
class Person extends Thread {
private String name;
public Person(String name) {
this.name = name;
}
@Override
public void run() {
for (int i = 1; i <= 10; i++) {
System.out.println("Hello, " + name + " " + i);
try {
Thread.sleep(100); // 대기시간을 생성 , 컴퓨터를 0.1초 멈추도록 한다.
} catch (Exception e) {
}
}
}
}
public class PersonTest {
public static void main(String[] args) {
Person p1 = new Person("홍길동");
Person p2 = new Person("이순신");
// p1.run(); //일반메소드로 호출하여 p1이 끝나야만 p2를 실행함.
// p2.run();
p1.start(); //둘이 공평하게 실행할 수 있게 끔
p2.start();
}
}
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
class RedBall extends JPanel {
@Override
protected void paintComponent(Graphics g) {
//빨간색으로 색상을 선택
g.setColor(Color.red);
//채운 원을 그리기.
g.fillOval(10, 220, 30, 30);
}
}
class MyFrame extends JFrame {
RedBall rb;
public MyFrame() {
rb = new RedBall();
add(rb);
setSize(400, 300);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
public class RedBallTest {
public static void main(String[] args) {
new MyFrame();
}
}
import practice01.Count.CountDownEvent;
//pdf 01번문제 (쓰레드연습)
class Count extends Thread {
protected int count;
public Count(int count) {
this.count = count;
}
@Override
public void run() {
for (int i = count; i >=1; i--) {
System.out.println(i + "초 전입니다.");
try {
Thread.sleep(1000);
} catch (Exception e) {
}
}
System.out.println("발사 !!!");
}
class CountDownEvent extends Count {
private int time;
private String msg;
public CountDownEvent(int count, int time) {
super(count);
this.time = time;
}
@Override
public void run() {
for (int i = 1; i <= 20; i--) {
System.out.println(i + "초 전입니다.");
try {
Thread.sleep(100);
} catch (Exception e) {
System.out.println("대기시간이 지났습니다.");
}
}
}
}
}
public class ThreadPractice {
public static void main(String[] args) {
Count c = new Count(20);
// CountDownEvent c1 = new CountDownEvent(20, 30, )
c.start();
}
}