求解JAVA编程题:编写一个应用程序,创建三个线程分别显示各自的运行时间

}new Thread(new Runnable() {public void run() {while(true) {if (s.get()==threadNum) {System.out.println(integer.get());break;}}}).start();}public static class MyThread implements Runnable {@Overridepublic void run() {long startTime = System.currentTimeMillis();try {Thre...
求解JAVA编程题:编写一个应用程序,创建三个线程分别显示各自的运行时间
import java.util.Calendar;
import java.util.concurrent.locks.Lock;

public class MultiThread {
static Lock mylock;
public static void main(String[] args) {
RunningObject1 r1 = new RunningObject1();

Thread t1 = new Thread(r1, "t1");
Thread t2 = new Thread(r1, "t2");
Thread t3 = new Thread(r1, "t3");
t1.start();
t3.start();
t2.start();
}

static class RunningObject1 implements Runnable {
public void run() {
synchronized(this){
String name=Thread.currentThread().getName();
System.out.println(name+"开始时间:"+Calendar.getInstance().getTimeInMillis());

for (int i = 0; i < 100000000; i++) {
if (i == 9999999) {
System.out.println(name+"结束时间:"+Calendar.getInstance().getTimeInMillis());
break;
}
}
}
}
}

}2010-11-24
public class ThreadE{
class Thread2 implements Runnable{
public void run(){
System.out.println(Thread.currentThread().getName()+" run "+Calendar.getInstance().getTime());
}
}

public static void main(String[] args) {
ThreadE te = new ThreadE();
new Thread(te.new Thread2()).start();
new Thread(te.new Thread2()).start();
new Thread(te.new Thread2()).start();
}
}2010-11-24
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()).start();}new Thread(new Runnable() {public void run() {while(true) {if (s.get()==threadNum) {System.out.println(integer.get());break;}}}}).start();}public static class MyThread implements Runnable {@Overridepublic void run() {long startTime = System.currentTimeMillis();try {Thread.sleep(new Random().nextInt(2000));} catch (InterruptedException e) {e.printStackTrace();}for (int i = 0; i < 10000000; i++) {integer.incrementAndGet();}System.out.println(Thread.currentThread().getName()+" running time "+(System.currentTimeMillis()-startTime+"ms"));s.incrementAndGet();}}}
2017-08-28
用Calendar的话,打印时间的地方就改为Calendar.getInstance().getTime()
我很奇怪,为什么代码贴不上来?? 图片也插入不了?

2010-11-23
import java.util.Calendar;
public class MyThread implements Runnable{
String name;
public MyThread(String name){
this.name = name;
}
@Override
public void run() {
Calendar time1 = Calendar.getInstance();
long t1 = time1.getTimeInMillis();
/*---------------------
线程体
-----------------------------------*/
Calendar time2 = Calendar.getInstance();
long t2 = time2.getTimeInMillis();
System.out.println("线程"+name+"运行时间为:"+(t2-t1)+"毫秒");
}

}
2017-08-24
public void run(){
Long start=System.currentTimeMillis();
线程代码;

Long end=System.currentTimeMillis();
System.out.println(Thread.currentThread().getName+"线程执行的时间是:"+(end-start)+"毫秒");
}2017-08-29
可以做到,你可以把多个线程同时运行的方法或代码块声明为synchronized的,这样的话,就不会相互影响了。具体可以查一下Synchronized的用法2017-08-27
mengvlog 阅读 80 次 更新于 2025-10-30 09:38:54 我来答关注问题0
  • anonymous 求解JAVA编程题:编写一个应用程序,创建三个线程分别显示各自的运行时间

    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...

  •  吹阴风点鬼火 java题目 编写一个应用程序,计算1~10之间各个整数的阶乘,并将结果输出到屏

    public static void main (String[] args){ long result = 1;for (int i = 1; i

  •  corruptsatan Java基础编程题求解

    import java.util.Scanner;/ author ww / public class Test { public static void main(String[] args) { Scanner s = new Scanner(System.in);System.out.println("输入一个正整数n(n>=2)");int n = s.nextInt();StringBuilder sb = new StringBuilder();boolean has = false;for(int ...

  •  磬石163 JAVA 编程题求教

    import java.awt.event.*;import java.awt.*;import javax.swing.*;public class List1 extends JFrame implements ActionListener { private JLabel lstName;private JTextField txfName;private JTextArea txaName;private JButton btnClose;public List1(){ lstName = new JLabel("请输入你的姓名:...

  •  terranlong JAVA编程题求答案

    Complex c1 = new Complex(1, 2);Complex c2 = new Complex(3, 4);Complex c3 = new Complex(0, 0);Complex c4 = new Complex(0, 0);c3 = c1.mul(c2);c1.show();System.out.print(" * ");c2.show();System.out.print(" = ");c3.show();System.out.println();c4 = c...

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

Java相关话题

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