}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