标签:
class 类名称 extends Thread{ //成员变量 //成员方法 public void run(){ //线程主体 } }
import java.util.Collection; import java.util.HashMap; import java.util.List; import java.util.Map; class myThread extends Thread { Map<String,? extends Collection<Integer>> map= new HashMap<String,List<Integer>>(); private String name; public myThread(String name){ this.name = name; } public void run(){ for(int i = 0; i < 10; i++){ System.out.println(name + "运行,i="+i); } } } public class MyThread { public static void main(String[] args){ myThread mt1 = new myThread("线程A"); myThread mt2 = new myThread("线程B"); mt1.start(); <span style="white-space:pre"> </span>mt2.start(); } }
public class Thread2 implements Runnable { //成员变量 //成员方法 public void run(){ //线程主体 } }小例子:
class A implements Runnable{ //成员变量 //成员方法 private String name; public A(String name){ this.name = name; } public void run(){ //线程主体 for(int i = 0; i < 10; i++){ System.out.println(name + "运行,i="+i); } } } public class Thread2{ public static void maim(String[] args){ A a1 = new A("线程A"); A a2 = new A("线程B"); Thread t1 = new Thread(a1); Thread t2 = new Thread(a2); t1.start(); t2.start(); } }
标签:
原文地址:http://blog.csdn.net/u012437355/article/details/45110985