多线程学习

时间:2020-07-07 22:10:06   收藏:0   阅读:76

一、线程的创建

1、继承thread类

public class MyThread extends Thread {
@Override
public void run() {
for (int i = 0; i < 200; i++) {
System.out.println("我在学习线程问题1......");
}
}

public static void main(String[] args) {
MyThread myThread = new MyThread();
myThread.start();


for (int i = 0; i < 2000; i++) {
System.out.println("我在学习线程问题");
}
}


}

2、实现runnable接口 无返回值

public class MyThread3 implements Runnable {
@Override
public void run() {
for (int i = 0; i < 200; i++) {
System.out.println("我在学习线程问题1......");
}
}

public static void main(String[] args) {

MyThread3 myThread3 = new MyThread3();
Thread thread = new Thread(myThread3);
thread.start();

for (int i = 0; i < 2000; i++) {
System.out.println("我在学习线程问题");
}
}


}

3、实现callable接口  有返回值

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!