首页>>帮助中心>>美国云服务器的java创建线程的方法有哪些

美国云服务器的java创建线程的方法有哪些

2023/8/29 468次

美国云服务器的java中创建线程的方法有:1.使用Runnable接口创建;2.使用Thread继承类创建;3.使用CallableFuture创建;

java中创建线程的方法有以下几种

1.使用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();

}

}

}

2.使用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();

}

}

}

}

3.使用CallableFuture创建

public interface Callable

{

  V call() throws Exception;

}

购买使用一诺网络美国云服务器,可以极大降低初创企业、中小企业以及个人开发者等用户群体的整体IT使用成本,无需亲自搭建基础设施、简化了运维和管理的日常工作量,使用户能够更专注于自身的业务发展和创新。美国云服务器低至49/月,购买链接:https://www.enuoidc.com/vpszq.html?typeid=3