1. 有两种方法可以创建并运行一个线程:
class MyThread extends Thread { MyThread() { // 调用下面代码,线程开始运行 start(); } @Override public void run() { // 这里的代码是在线程中执行的 int i = 0; while (i < 20) { System.out.println(i); try { sleep(100); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } ++i; } } } public class Main { public static void main(String[] args) { MyThread myThread = new MyThread(); } }
// 实现Runnable接口 public class Main implements Runnable { @Override public void run() { // 这里的代码是在线程中执行的 int i = 0; while (i < 20) { System.out.println(i); try { Thread.sleep(100); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } ++i; } } public static void main(String[] args) { Main runable = new Main(); new Thread(runable).start(); } }
class MyThread extends Thread { MyThread() { start(); } @Override public void run() { synProc(); } synchronized public void synProc(){ int count = 0; while (count++ < 10) { try { sleep(100); System.out.println("synProc: " + count); } catch (InterruptedException e) { e.printStackTrace(); } } } } public class Main{ public static void main(String[] args) { MyThread thread = new MyThread(); thread.synProc(); } } // 输出结果是: synProc: 1 synProc: 2 synProc: 3 synProc: 4 synProc: 5 synProc: 6 synProc: 7 synProc: 8 synProc: 9 synProc: 10 synProc: 1 synProc: 2 synProc: 3 synProc: 4 synProc: 5 synProc: 6 synProc: 7 synProc: 8 synProc: 9 synProc: 10
class MyThread extends Thread { MyThread() { start(); } @Override public void run() { synProc(); } synchronized public void synProc(){ int count = 0; while (count++ < 10) { try { sleep(100); System.out.println("synProc: " + count); } catch (InterruptedException e) { e.printStackTrace(); } } } synchronized public void synProc2() { int count = 0; while (count++ < 10) { try { sleep(100); System.out.println("synProc2: " + count); } catch (InterruptedException e) { e.printStackTrace(); } } } } public class Main{ public static void main(String[] args) { MyThread thread = new MyThread(); thread.synProc2(); } } // 输出 synProc2: 1 synProc2: 2 synProc2: 3 synProc2: 4 synProc2: 5 synProc2: 6 synProc2: 7 synProc2: 8 synProc2: 9 synProc2: 10 synProc: 1 synProc: 2 synProc: 3 synProc: 4 synProc: 5 synProc: 6 synProc: 7 synProc: 8 synProc: 9 synProc: 10
class MyThread extends Thread { MyThread() { start(); } @Override public void run() { synProc(); } public void synProc(){ int count = 0; synchronized (this) { do { try { sleep(50); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("synProc :" + count); } while (count++ < 3); } } synchronized public void synProc2() { int count = 0; do { try { sleep(50); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("synProc2 :" + count); } while (count++ < 3); } } public class Main{ public static void main(String[] args) { MyThread thread = new MyThread(); thread.synProc2(); } } // 输出: synProc2 :0 synProc2 :1 synProc2 :2 synProc2 :3 synProc :0 synProc :1 synProc :2 synProc :3
5. 线程之间的等待与通知通过wait和notify,notifyAll来实现。
下面代码演示了一个writer和多个reader的协作过程:
class Writer extends Thread { private char chr; @Override public void run() { for (char c = 'A'; c <= 'Z'; ++c){ chr = c; // 通知Reader可以读了 synchronized (this) { notifyAll(); } // 过会儿再写 try { sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } } } public char getChar() { return chr; } } class Reader extends Thread { private Writer writer; Reader(Writer writer) { this.writer = writer; start(); } @Override public void run() { while (true){ // 等待Writer写 synchronized (writer) { try { // 此处Reader线程将被挂起 writer.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } // 读取Writer写的内容 char chr = writer.getChar(); System.out.print(chr + " "); // 读到Z即结束 if (chr == 'Z') { System.out.println(); break; } } } } public class Main{ public static void main(String[] args) { Writer writer = new Writer(); Reader reader1 = new Reader(writer); Reader reader2 = new Reader(writer); // 开始 writer.start(); } } // 最后的输出是: A A B B C C D D E E F F G G H H I I J J K K L L M M N N O O P P Q Q R R S S T T U U V V W W X X Y Y Z Z