ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、视频、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
方法一:继承thread ~~~ package thread.a1; public class Test { public static void main(String[] args) { Xc xc = new Xc(); //xc.run(); xc.start(); //谁调用的start方法,程序就去自动调用run方法 //start会但开启一个线程,而不是直接调用。 //xc.start(); for(int i=0;i<20;i++) { System.out.println("主函数"); } } } class Xc extends Thread //创建线程所需要继承的类 { public void run() //run方法是覆盖的父类方法 { for(int i=0;i<20;i++) { System.out.println("子函数"); } } } ~~~ 方法二:实现Runnable接口 ~~~ package thread.a1; public class Test1 { public static void main(String[] args) { Xc2 xc2=new Xc2(); Thread a=new Thread(xc2); a.start(); for(int i=0;i<20;i++) { System.out.println("主函数"); } } } class Xc2 implements Runnable //不继承类,而是改成了实现接口 { public void run() { for(int i=0;i<20;i++) { System.out.println("子函数"); } } } ~~~