1 스레드 객체
Java 코드
Thread t = new Thread( new Thread(){ @Override public void run() { while(true){ try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("1: "+Thread.currentThread().getName()); System.out.println("2: "+this.getName()); } } } ); t.start();
2 실행 가능한 인터페이스
Java 코드
Thread t2 = new Thread( new Runnable(){ @Override public void run() { while(true){ try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("3: "+Thread.currentThread().getName()); } } } ); t2.start();
코드의 어느 부분이 실행될까요?
Java 코드
//只要重写了run方法,你从构造函数传递进去的线程对象就不会在执行 new Thread(new Runnable(){ @Override public void run() { while(true){ try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("runnable: "+Thread.currentThread().getName()); } } } ){ @Override public void run() { while(true){ try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("thread: "+Thread.currentThread().getName()); } } }.start();
출력 결과는 다음과 같습니다.
로그 코드
3: Thread-2 1: Thread-1 2: Thread-0 thread: Thread-3
다음 코드 출처: JAVA 프로그래밍 사고 4판에서는 스레드 추가 여부에 대해 설명합니다.
Counter1.java는 스레드 추가 성능을 보여주지 않습니다.
Java 코드
import java.awt.*; import java.awt.event.*; import java.applet.*; public class Counter1 extends Applet { private static final long serialVersionUID = 1L; private int count = 0; private Button onOff = new Button("Toggle"), start = new Button("Start"); private TextField t = new TextField(10); private boolean runFlag = true; public void init() { add(t); start.addActionListener(new StartL()); add(start); onOff.addActionListener(new OnOffL()); add(onOff); } @SuppressWarnings("static-access") public void go() { while (true) { try { Thread.currentThread().sleep(100); } catch (InterruptedException e) { } if (runFlag) t.setText(Integer.toString(count++)); } } class StartL implements ActionListener { public void actionPerformed(ActionEvent e) { go(); } } class OnOffL implements ActionListener { public void actionPerformed(ActionEvent e) { runFlag = !runFlag; } } public static void main(String[] args) { Counter1 applet = new Counter1(); Frame aFrame = new Frame("Counter1"); aFrame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); aFrame.add(applet, BorderLayout.CENTER); aFrame.setSize(300, 200); applet.init(); applet.start(); aFrame.setVisible(true); } }
Counter2i.java 스레드를 추가한 후 결과가 상당히 다릅니다. Thread를 상속하는 내부 클래스를 추가했습니다.
Java 코드
import java.awt.*; import java.awt.event.*; import java.applet.*; public class Counter2i extends Applet { private static final long serialVersionUID = 1L; private class SeparateSubTask extends Thread { int count = 0; boolean runFlag = true; SeparateSubTask() { start(); } public void run() { while (true) { try { sleep(100); } catch (InterruptedException e) { } if (runFlag) t.setText(Integer.toString(count++)); } } } private SeparateSubTask sp = null; private TextField t = new TextField(10); private Button onOff = new Button("Toggle"), start = new Button("Start"); public void init() { add(t); start.addActionListener(new StartL()); add(start); onOff.addActionListener(new OnOffL()); add(onOff); } class StartL implements ActionListener { public void actionPerformed(ActionEvent e) { if (sp == null) sp = new SeparateSubTask(); } } class OnOffL implements ActionListener { public void actionPerformed(ActionEvent e) { if (sp != null) sp.runFlag = !sp.runFlag; // invertFlag(); } } public static void main(String[] args) { Counter2i applet = new Counter2i(); Frame aFrame = new Frame("Counter2i"); aFrame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); aFrame.add(applet, BorderLayout.CENTER); aFrame.setSize(300, 200); applet.init(); applet.start(); aFrame.setVisible(true); } }
Counter3.java Runnable 인터페이스
Java 코드
import java.awt.*; import java.awt.event.*; import java.applet.*; public class Counter3 extends Applet implements Runnable { private static final long serialVersionUID = 1L; private int count = 0; private boolean runFlag = true; private Thread selfThread = null; private Button onOff = new Button("Toggle"), start = new Button("Start"); private TextField t = new TextField(10); public void init() { add(t); start.addActionListener(new StartL()); add(start); onOff.addActionListener(new OnOffL()); add(onOff); } @SuppressWarnings("static-access") public void run() { while (true) { try { selfThread.sleep(100); } catch (InterruptedException e) { } if (runFlag) t.setText(Integer.toString(count++)); } } class StartL implements ActionListener { public void actionPerformed(ActionEvent e) { if (selfThread == null) { selfThread = new Thread(Counter3.this); selfThread.start(); } } } class OnOffL implements ActionListener { public void actionPerformed(ActionEvent e) { runFlag = !runFlag; } } public static void main(String[] args) { Counter3 applet = new Counter3(); Frame aFrame = new Frame("Counter3"); aFrame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); aFrame.add(applet, BorderLayout.CENTER); aFrame.setSize(300, 200); applet.init(); applet.start(); aFrame.setVisible(true); } }
Java 스레드와 관련된 추가 기사(익명) 내부 클래스), PHP 중국어 웹사이트를 팔로우하세요!