💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、豆包、星火、月之暗面及文生图、文生视频 广告
方法一:继承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("子函数"); } } } ~~~