java中创建线程的方法有:1.使用Runnable接口创建;2.使用Thread继承类创建;3.使用Callable和Future创建;java中创建线程的方法有以下几种使用Runnable接口创建public class Runnabl
java中创建线程的方法有:1.使用Runnable接口创建;2.使用Thread继承类创建;3.使用Callable和Future创建;
java中创建线程的方法有以下几种
使用Runnable接口创建
public class RunnableThreadTest extends *** implements Runnable {
private int i;
public void run() {
for (i = 0; i < 100; i++) {
System.out.println(Thread.currentThread().getName() + " " + i);
}
}
public static void main(String[] args) {
for (int i = 0; i < 100; i++) {
System.out.println(Thread.currentThread().getName() + " " + i);
if (i == 20) {
RunnableThreadTest rtt = new RunnableThreadTest();
new Thread(rtt, "新线程1").start();
new Thread(rtt, "新线程2").start();
}
}
}
使用Thread继承类创建
public class ThreadTest extends Thread {
int i = 0;
public void run() {
for (; i < 100; i++) {
System.out.println(getName() + " " + i);
}
}
public static void main(String[] args) {
for (int i = 0; i < 100; i++) {
System.out.println(Thread.currentThread().getName() + " : " + i);
if (i == 50) {
new ThreadTest().start();
new ThreadTest().start();
}
}
}
}
使用Callable和Future创建
public interface Callable
{
V call() throws Exception;
}
--结束END--
本文标题: java创建线程的方法有哪些
本文链接: https://www.lsjlt.com/news/111198.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-05-11
2024-05-11
2024-05-11
2024-05-11
2024-05-11
2024-05-11
2024-05-11
2024-05-11
2024-05-11
2024-05-11
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0