1.Java产生线程有两种方法:一是,继承Thread类,且覆盖其run方法;二是,实现Runnable接口,并将实现类对象作为参数传递给Thread类的构造方法

//重写run方法
public class HelloJava extends Thread{

	public void run(){
		System.out.println("This is anther thread");
	}
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Thread NewThread = new HelloJava();
		NewThread.run();
		System.out.println("This is main thread");
	}

}


//利用Runnable接口实现
public class HelloJava implements Runnable{

	public void run(){
		System.out.println("This is anther thread");
	}
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		HelloJava Object = new HelloJava();
		Thread NewThread = new Thread(Object);
		NewThread.start();
		System.out.println("This is main thread");
	}
}


相关文章:

  • 2021-07-15
  • 2021-09-09
  • 2022-12-23
  • 2021-09-11
  • 2022-02-09
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-12-25
  • 2021-06-11
  • 2018-07-28
  • 2021-12-30
  • 2022-02-09
相关资源
相似解决方案