public class ThreadTest { public static void main(String[] args) { new EvenPrinter().start(); new EvenPrinter().start(); } } /** * 打印100以内的偶数 */ class EvenPrinter extends Thread { @Override public void run() { for (int i = 1; i <= 100; i++) { if (i % 2 == 0) { System.out.println(Thread.currentThread().getName() + ":" + i); } } } }
public class ThreadTest { public static void main(String[] args) { OddPrinter oddPrinter = new OddPrinter(); new Thread(oddPrinter).start(); new Thread(oddPrinter).start(); new Thread(oddPrinter).start(); } } /** * 打印100以内的奇数 */ class OddPrinter implements Runnable { @Override public void run() { for (int i = 1; i <= 100; i++) { if (i % 2 != 0) { System.out.println(Thread.currentThread().getName() + ":" + i); } } } }
与实现Runnable接口相比,实现Callable这种方式:
public class ThreadTest { public static void main(String[] args) { NumberPrinter numberPrinter = new NumberPrinter(); FutureTask<Integer> task = new FutureTask<>(numberPrinter); new Thread(task).start(); Integer sum = null; try { sum = task.get(); } catch (InterruptedException e) { e.printStackTrace(); } catch (ExecutionException e) { e.printStackTrace(); } System.out.println("sum=" + sum); } } /** * 打印100以内的数字,并返回其和 */ class NumberPrinter implements Callable<Integer> { @Override public Integer call() throws Exception { int sum = 0; for (int i = 1; i <= 100; i++) { System.out.println(Thread.currentThread().getName() + ":" + i); sum += i; } return sum; } }
public class ThreadTest { public static void main(String[] args) { // 创建一个包含10个线程的线程池 ExecutorService service = Executors.newFixedThreadPool(10); // void execute(Runnable command) service.execute(new OddPrinter()); service.execute(new EvenPrinter()); // <T> Future<T> submit(Callable<T> task) Future<Integer> future = service.submit(new NumberPrinter()); Integer sum = 0; try { sum = future.get(); } catch (InterruptedException e) { e.printStackTrace(); } catch (ExecutionException e) { e.printStackTrace(); } System.out.println("sum=" + sum); // 关闭线程池 service.shutdown(); } } /** * 打印100以内的数字,并返回其和 */ class NumberPrinter implements Callable<Integer> { @Override public Integer call() throws Exception { int sum = 0; for (int i = 1; i <= 100; i++) { System.out.println(Thread.currentThread().getName() + ":" + i); sum += i; } return sum; } } /** * 打印100以内的奇数 */ class OddPrinter implements Runnable { @Override public void run() { for (int i = 1; i <= 100; i++) { if (i % 2 != 0) { System.out.println(Thread.currentThread().getName() + ":" + i); } } } } /** * 打印100以内的偶数 */ class EvenPrinter extends Thread { @Override public void run() { for (int i = 1; i <= 100; i++) { if (i % 2 == 0) { System.out.println(Thread.currentThread().getName() + ":" + i); } } } }
public class ThreadTest { public static void main(String[] args) { OddPrinter oddPrinter = new OddPrinter(); Thread t1 = new Thread(oddPrinter); // 设置线程名称 t1.setName("线程t1"); // 启动线程t1 t1.start(); // 等待线程t1执行完毕后,再继续执行后面的代码 try { t1.join(); } catch (InterruptedException e) { e.printStackTrace(); } // 线程的状态(生命周期) System.out.println("t1.getState():" + t1.getState()); // 线程是否存活 System.out.println("t1.isAlive():" + t1.isAlive()); } } /** * 打印100以内的奇数 */ class OddPrinter implements Runnable { @Override public void run() { for (int i = 1; i <= 100; i++) { if (i % 2 != 0) { // 让当前线程休眠100毫秒 try { Thread.currentThread().sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(Thread.currentThread().getName() + ":" + i); } } } }
public class TicketWindowTest { public static void main(String[] args) { TicketWindow ticketWindow = new TicketWindow(); new Thread(ticketWindow, "窗口1").start(); new Thread(ticketWindow, "窗口2").start(); new Thread(ticketWindow, "窗口3").start(); } } /** * 售票窗口 */ class TicketWindow implements Runnable { // 总共100张票 private int ticket = 100; @Override public void run() { while (true) { // 使用this作为同步监视器(锁) synchronized (this) { if (ticket > 0) { try { Thread.sleep(10); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(Thread.currentThread().getName() + ":" + ticket--); } else { break; } } } } }
public class TicketWindowTest { public static void main(String[] args) { new TicketWindow().start(); new TicketWindow().start(); new TicketWindow().start(); } } /** * 售票窗口 */ class TicketWindow extends Thread { // 总共100张票 private static int ticket = 100; @Override public void run() { while (true) { // 使用TicketWindow.class作为同步监视器(锁) synchronized (TicketWindow.class) { if (ticket > 0) { try { Thread.sleep(10); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(Thread.currentThread().getName() + ":" + ticket--); } else { break; } } } } }
public class TicketWindowTest { public static void main(String[] args) { TicketWindow ticketWindow = new TicketWindow(); new Thread(ticketWindow, "窗口1").start(); new Thread(ticketWindow, "窗口2").start(); new Thread(ticketWindow, "窗口3").start(); } } /** * 售票窗口 */ class TicketWindow implements Runnable { // 总共100张票 private int ticket = 100; @Override public void run() { while (ticket > 0) { sale(); } } /** * sale方法不是静态的,将使用this作为同步监视器 */ private synchronized void sale() { if (ticket > 0) { try { Thread.sleep(10); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(Thread.currentThread().getName() + ":" + ticket--); } } }
public class TicketWindowTest { public static void main(String[] args) { new TicketWindow().start(); new TicketWindow().start(); new TicketWindow().start(); } } /** * 售票窗口 */ class TicketWindow extends Thread { // 总共100张票 private static int ticket = 100; @Override public void run() { while (ticket > 0) { sale(); } } /** * sale方法是静态的,将使用TicketWindow.class作为同步监视器 */ private static synchronized void sale() { if (ticket > 0) { try { Thread.sleep(10); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(Thread.currentThread().getName() + ":" + ticket--); } } }
public class TicketWindowTest { public static void main(String[] args) { TicketWindow ticketWindow = new TicketWindow(); new Thread(ticketWindow, "窗口1").start(); new Thread(ticketWindow, "窗口2").start(); new Thread(ticketWindow, "窗口3").start(); } } /** * 售票窗口 */ class TicketWindow implements Runnable { // 总共100张票 private int ticket = 100; // 同步监视器 private ReentrantLock lock = new ReentrantLock(true); @Override public void run() { while (ticket > 0) { lock.lock(); if (ticket > 0) { try { Thread.sleep(10); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(Thread.currentThread().getName() + ":" + ticket--); } lock.unlock(); } } }
三个方法:
注意点:
public class CommTest { public static void main(String[] args) { // 两个线程将交替打印 NumberPrinter numberPrinter = new NumberPrinter(); new Thread(numberPrinter).start(); new Thread(numberPrinter).start(); } } /** * 打印1~100 */ class NumberPrinter implements Runnable { private int i = 1; @Override public void run() { while (true) { synchronized (this) { if (i <= 100) { // 唤醒一个线程 notify(); System.out.println(Thread.currentThread().getName() + ":" + i++); // 休眠当前线程 try { wait(); } catch (InterruptedException e) { e.printStackTrace(); } } else { // 唤醒所有线程,否则程序无法自动退出 notifyAll(); break; } } } } }