继承Thead
class IphoneThread extends Thread { public int iphone = 5; public String user; public IphoneThread(String str) { user = str; } @Override public void run() { while (iphone != 0) { iphone--; System.out.println(user + " get one and left iphone num=" + iphone); } } } public class ThreadTest { public static void main(String[] arg) { IphoneThread t1 = new IphoneThread("Toms"); t1.start(); IphoneThread t2 = new IphoneThread("Jack"); t2.start(); IphoneThread t3 = new IphoneThread("Boom"); t3.start(); } }
实现Runnable接口
class IphoneRunable implements Runnable { public int iphone = 5; @Override public void run() { while (iphone != 0) { iphone--; System.out.println(Thread.currentThread().getName() + " get one and left iphone num=" + iphone); } } } public class RunableTest { public static void main(String[] arg) { IphoneRunable runable = new IphoneRunable(); Thread t1 = new Thread(runable, "Toms"); t1.start(); Thread t2 = new Thread(runable, "Jack"); t2.start(); Thread t3 = new Thread(runable, "Boom"); t3.start(); } }
时间: 2025-01-02 01:13:40