7个回答
2017-08-28
展开全部
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 {
@Override
public 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();
}
}
}
展开全部
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;
}
}
}
}
}
}
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;
}
}
}
}
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
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();
}
}
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();
}
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
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)+"毫秒");
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询