Java 멀티스레드가 있는 예제

1 개요[ | ]

Java 멀티스레드가 있는 예제

2 예제 1[ | ]

public class MyThread extends Thread {
    private int number;

    public MyThread(int number) {
        this.number = number;
    }

    @Override
    public void run() {
        System.out.println(number);
    }
}
public class Main {
    public static void main(String[] args) throws InterruptedException {
        System.out.println("[main] started.");
        int numbers[] = { 7, 5, 9, 2, 5, 2 };
        for (int number : numbers) {
            MyThread thread = new MyThread(number);
            thread.start();
        }
        System.out.println("[main] all threads started.");
        System.out.println("[main] finished.");
    }
}

3 예제 2[ | ]

  • sleep을 넣어서 결과적으로 정렬된 결과로 보인다.
public class MyThread extends Thread {
    private int number;

    public MyThread(int number) {
        this.number = number;
    }

    @Override
    public void run() {
        try {
            Thread.sleep(100 * number);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(number);
    }
}
public class Main {
    public static void main(String[] args) throws InterruptedException {
        System.out.println("[main] started.");
        int numbers[] = { 7, 5, 9, 2, 5, 2 };
        for (int number : numbers) {
            MyThread thread = new MyThread(number);
            thread.start();
        }
        System.out.println("[main] all threads started.");
        System.out.println("[main] finished.");
    }
}

4 예제 3[ | ]

  • Graphics2D는 스레드안전하기 때문에, 스레드에서 사용할 수는 있다.
  • 다만 가끔 엉뚱한 색이 찍힌다.
  • 21행과 22행 사이에 다른 스레드의 영향을 받은 것
import java.awt.Color;
import java.awt.Graphics2D;

public class MyThread extends Thread {
    private Graphics2D g;
    private int x;
    private int y;
    private int size;
    private Color color;

    public MyThread(Graphics2D g, int x, int y, int size, Color color) {
        this.g = g;
        this.x = x;
        this.y = y;
        this.size = size;
        this.color = color;
    }

    @Override
    public void run() {
        g.setColor(color);
        g.fillRect(x, y, size, size);
    }
}
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;

public class Main {
    public static void main(String[] args) throws IOException, InterruptedException {
        BufferedImage bufferedImage = new BufferedImage(400, 400, BufferedImage.TYPE_INT_RGB);
        Graphics2D g = bufferedImage.createGraphics();
        g.fillRect(0, 0, 400, 400);
        for (int i = 0; i < 80; i++) {
            for (int j = 0; j < 80; j++) {
                int x = i * 5;
                int y = j * 5;
                Color color = ((i + j) % 2 == 0) ? Color.BLUE : Color.GREEN;
                MyThread thread = new MyThread(g, x, y, 5, color);
                thread.start();
            }
        }
        g.dispose();
        ImageIO.write(bufferedImage, "png", new File("myimage.png"));
    }
}

5 같이 보기[ | ]

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}