Thread类的构造方法
publicThread() publicThread(Runnabletarget)
通过继承Thread类创建线程
第二部分:
通过继承Thread类、重写run()方法而创建线程
classSubThreadextendsThread{ publicvoidrun(){ for (inti=1; i<=20; i++) System.out.print("次"+i+", "); } } publicclassTestExtendsThread{ publicstaticvoidmain(String[] args){ SubThreadsub=newSubThread(); sub.start(); for (inti=1; i<=20; i++) System.out.print("主"+i+", "); } }
通过实现Runnable接口创建线程
第三部分:
1.Runnable是java.lang包中的接口
publicinterfaceRunnable{ publicabstractvoidrun(); }
2.某个类实现Runnable接口,实现其方法run()后,可以把这个类创建的对象传递给 public Thread(Runnable target)中的接口变量target,从而创建线程
classRunClassimplementsRunnable{ publicvoidrun(){ for (inti=1; i<=20; i++) System.out.print("次"+i+", "); } } publicclassTestImplementsRunnable{ publicstaticvoidmain(String[] args){ Threadsub=newThread(newRunClass()); sub.start(); for (inti=1; i<=20; i++) System.out.print("主"+i+", "); } }