java之多线程
程序员文章站
2023-12-23 15:31:58
...
java实现多线程的两种方法:
package io;
public class TextThread {
public static void main(String[] args) {
//调用继承Thread接口的类
myThread mt=new myThread();
mt.start();
//调用实现Runnable接口的类
myRunnable mr=new myRunnable();
Thread thread=new Thread(mr);
thread.start();
}
}
//我是继承Thread接口的类
class myThread extends Thread{
@Override
public void run() {
// TODO Auto-generated method stub
super.run();
for(int i=0;i<100;i++) {
System.out.println("我是继承Thread接口实现的线程"+i);
}
}
}
//我是实现Runnable接口的类
class myRunnable implements Runnable {
@Override
public void run() {
// TODO Auto-generated method stub
for(int i=0;i<100;i++) {
System.out.println("我是实现Runnable接口的线程"+i);
}
}
}