package com.andy.thread;
/*
* 线程实现的两种方式
* 1、继承Thread类
* 2、实现Runnable接口
*
*/
public class ThreadDemo {
public static void main(String[] args){
//继承Thread类
MyThread mythread =new MyThread();
mythread.start();
for(int a=0;a<10;a++){
System.out.println("a=="+a);
}
//实现Runnable接口
MyRunnable runnable=new MyRunnable();
Thread thread=new Thread(runnable);
thread.start();
}
}
class MyThread extends Thread{
@Override
public void run() {
for(int i=0;i<10;i++){
System.out.println("i=="+i);
}
super.run();
}
}
class MyRunnable implements Runnable{
@Override
public void run(){
for(int b=0;b<10;b++){
System.out.println("b=="+b);
}
}
}
/*
* 线程实现的两种方式
* 1、继承Thread类
* 2、实现Runnable接口
*
*/
public class ThreadDemo {
public static void main(String[] args){
//继承Thread类
MyThread mythread =new MyThread();
mythread.start();
for(int a=0;a<10;a++){
System.out.println("a=="+a);
}
//实现Runnable接口
MyRunnable runnable=new MyRunnable();
Thread thread=new Thread(runnable);
thread.start();
}
}
class MyThread extends Thread{
@Override
public void run() {
for(int i=0;i<10;i++){
System.out.println("i=="+i);
}
super.run();
}
}
class MyRunnable implements Runnable{
@Override
public void run(){
for(int b=0;b<10;b++){
System.out.println("b=="+b);
}
}
}
本文介绍了Java中创建线程的两种常见方式:继承Thread类与实现Runnable接口,并通过具体示例代码展示了这两种方式的使用方法。
2149

被折叠的 条评论
为什么被折叠?



