方式一:
package com.Thread1;public class myRunable implements Runnable {public int count;@Overridepublic void run() {while (count < 100) {count++;System.out.package com.Thread1;public class myRunableTest {public static void main(String[] args) {Thread t1 = new Thread(new myRunable(), "線程1");t1.start();Thread t2 = new Thread(new myRunable(), "線程2");t2.start();}}方式二:
package com.Thread1;public class MyThread1 extends Thread {public int count;@Overridepublic void run() {while (count <100) {count++;System.out.println("count:"+count+"由"+Thread.currentThread().getName()+"創建");}}}package com.Thread1;public class MyThread1Test {public static void main(String[] args) {// MyThread1 mt1 = new MyThread1();// Thread t = new Thread(mt1);// t.start();Thread t1 = new MyThread1();t1.setName("線程1");t1.start();Thread t2 = new MyThread1();t2.setName("線程2");t2.start();}}
新聞熱點
疑難解答