多线程学习1

来源:互联网 发布:农村广电网络宽带 编辑:程序博客网 时间:2024/04/28 22:19

 

继承 Thread

package thread;/** * <pre> * 继承 Thread * </pre> */public class ThreadTest extends Thread {public ThreadTest(String name) {super(name);}public void run() {for (int x = 0; x < 60; x++) {System.out.println((Thread.currentThread() == this) + ", " + this.getName()+ " run..." + x);}}public static void main(String[] args) {ThreadTest t1 = new ThreadTest("one--");ThreadTest t2 = new ThreadTest("two");t1.start();t2.start();// t1.run();// t2.run();for (int x = 0; x < 60; x++) {System.out.println("main..." + x);}}}


 

 实现Runnable 接口 

package runnable;/** * <pre> * 实现Runnable 接口 *</pre> */public class RunnableDemo implements Runnable {public void run() {for (int i = 0; i < 100; i++) {System.out.println("线程-->" + i);}}public static void main(String[] args) {RunnableDemo runnableDemo = new RunnableDemo();Thread t = new Thread(runnableDemo);t.start();for (int i = 0; i < 100; i++) {System.out.println("main-->" + i);}}}