用java编程:创建三个线程,分别显示各自的开始运行时间及总运行时间

} public void run() { long startTime = System.currentMillis();System.out.println(name + " start at : " + startTime);Thread.sleep(1000);System.out.println(name + " totoal cost : " + (System.currentMillis() - startTime));} } public class Test { public static void main...
用java编程:创建三个线程,分别显示各自的开始运行时间及总运行时间
public class AThread extends Thread {
private String name;

public AThread(String name) {
this.name = name;
}
public void run() {
long startTime = System.currentMillis();
System.out.println(name + " start at : " + startTime);
Thread.sleep(1000);
System.out.println(name + " totoal cost : " + (System.currentMillis() - startTime));
}
}

public class Test {
public static void main(String[] args) {
for (int i = 0; i < 3; i++) {
AThread t = new AThread("thread " + i);
t.start();
}
}
}2009-04-24
mengvlog 阅读 40 次 更新于 2025-10-30 03:54:32 我来答关注问题0
  • public class ThreadRuningTime {public static AtomicInteger integer = new AtomicInteger(0);public static AtomicInteger s = new AtomicInteger(0);public static int threadNum = 3;public static void main(String[] args) {for (int i = 0; i < threadNum; i++) {new Thread(new MyThread...

  •  xiaofeizm55333 Java语言:Java多线程怎样创建

    程序中定义一个线程类,它扩展了Thread类。利用扩展的线程类在MutliThreadDemo类的主方法中创建了三个线程对象,并通过start()方法分别将它们启动。从结果可以看到,每个线程分别对应100张电影票,之间并无任何关系,这就说明每个线程之间是平等的,没有优先级关系,因此都有机会得到CPU的处理。但是结果显示...

  •  大雨滂沱踩地雷 如何创建线程?如何保证线程安全?

    创建线程的方式一:继承Thread类(由于Java单继承的特性,这种方式用的比较少)步骤:1、继承Thread类,然后重写run方法 请点击输入图片描述 2、创建子类对象,然后调用start()方法来启动线程 请点击输入图片描述 我们可以看到这边现在只创建了一个线程,那么如果要创建多个线程要怎么做呢?通过继承Thread的方...

  •  阿暄生活 java多线程是什么

    创建一个新的类实现Runnable接口。实现Runnable接口中的run()方法,该方法同样包含线程要执行的代码。将Runnable接口的实例作为参数传递给Thread类的构造函数,创建Thread类的实例。调用Thread实例的start()方法来启动线程。三、多线程的优势与注意事项 优势:提高程序性能:通过并发执行多个任务,充分利用多核处...

  •  信必鑫服务平台 多线程的java 程序如何编写?

    Java 给多线程编程提供了内置的支持。 一条线程指的是进程中一个单一顺序的控制流,一个进程中可以并发多个线程,每条线程并行执行不同的任务。新建状态:使用 new 关键字和 Thread 类或其子类建立一个线程对象后,该线程对象就处于新建状态。它保持这个状态直到程序 start() 这个线程。就绪状态:当线程...

檬味博客在线解答立即免费咨询

Java相关话题

Copyright © 2023 WWW.MENGVLOG.COM - 檬味博客
返回顶部