//方式1:继承Thread类
//播放音乐线程类
class MusicThread extends Thread{
public void run() {
for (int i = 0; i <50; i++) {
System.out.println("我在听音乐!"+i);
}
}
}
public class Thread_create {
public static void main(String[] args) {
for (int i = 0; i < 50; i++) {
System.out.println("我在打游戏"+i);
if(i==10){
//创建线程并启动对象
MusicThread music=new MusicThread();
// music.run();
music.start();//不能调用run方法
}
}
}
}
//实现Runnable接口
//播放音乐的类
class RunnableMusic implements Runnable{
public void run() {
for (int i = 0; i <50; i++) {
System.out.println("我在听音乐!"+i);
}
}
}
public class ImplementsRunnable {
public static void main(String[] args) {
//运行游戏
for (int i = 0; i < 50; i++) {
System.out.println("我在打游戏"+i);
if(i==10){
//创建线程并启动对象
Runnable r=new RunnableMusic();
Thread runnable= new Thread(r);
runnable.start();
}
}
}
}
继承Thread类和实现接口Runnable之间的区别?