求大神解决下多线程的问题

qq_32989533 2019-09-08 04:29:26
求大神帮忙解决下,在主方法里创建了100个线程,在run方法里让每个线程执行100次,如果实现每执行100次输出下执行时间,代码如下:
package test1;

public class test implements Runnable{
public static void main(String[] args){
Runnable ra = new test();
for(int i = 0; i < 100; i++){
Thread thread = new Thread(ra);
thread.start();
}
}

@Override
public void run() {
int sum = 0;
for(int i = 0; i < 100 ; i++){
sum += i;
System.out.println(sum);
}
long end = System.currentTimeMillis();
}

}
...全文
146 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
qq_39936465 2019-09-09
  • 打赏
  • 举报
回复
引用 2 楼 qq_32989533 的回复:
那我该如何实现每执行100次,输出下执行时间呢
100个线程执行100次太快了,所以我在程序中间加入了sleep,不然基本都是0;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class test6 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Sum sum=new Sum();
		for(int i=0;i<100;i++) {
			new testThread(sum).start();
		}
	}
}

class Sum {
	private int s = 1;
	private long start, end;
	private Lock lock=new ReentrantLock();

	/**
	 * 
	 */
	public Sum() {
		super();
		this.start = System.currentTimeMillis();
	}

	public  int getSum() {
		return s;
	}

	public  void setSum() {
		this.s = s+1;
	}

	public long getStart() {
		return start;
	}

	public void setStart(long start) {
		this.start = start;
	}

	public  long getEnd() {
		return end;
	}

	public void setEnd(long end) {
		this.end = end;
	}

	@Override
	public String toString() {
		return "第" + getSum() / 100 + "个" +"100次耗时"+(end-start);
	}
	
	public void culc() {
		lock.lock();
		if (getSum() % 100 == 0) {
			setEnd(System.currentTimeMillis());
			System.out.println(toString());
			setStart(System.currentTimeMillis());
		}
		setSum();
		lock.unlock();
	}
	
}

class testThread extends Thread{
	private Sum sum;
	private Lock lock=new ReentrantLock();
		
	/**
	 * @param sum
	 */
	public testThread(Sum sum) {
		super();
		this.sum = sum;
	}

	@Override
	public void run() {
		// TODO Auto-generated method stub
		for (int i = 0; i < 100; i++) {
			sum.culc();
			try {
				Thread.sleep(100);//数100太快了加入了等待
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}

Motivation丶R 2019-09-09
  • 打赏
  • 举报
回复
public class MyRunnable implements Runnable { @Override public void run() { int index = 100; long startTime = 0; while (true){ if(index == 100){ startTime = System.currentTimeMillis(); } //TODO 业务逻辑 if(index-- == 0){ System.out.println(Thread.currentThread().getName() + "执行时间:" + (System.currentTimeMillis() - startTime)); index = 100; } } } public static void main(String[] args) { for(int i = 0; i < 100; i++){ new Thread(new MyRunnable(), "Thread" + i).start(); } } }
Theseuss 2019-09-08
  • 打赏
  • 举报
回复
1、Thread thread = new Thread( new test()); 2、主线程中你不知道什么时候你创建的100个线程执行完,要么使用回调通知,要么你的test实现Callable,让每个都返回给你一个执行完成时候的时间放到List<Future<Long>>,然后你得到最大值即可,或者写一个监控线程,当最后一个线程执行完的时候输出时间,最简单的就是按照你的方式打印到屏幕,你看最后一行的时间
qq_32989533 2019-09-08
  • 打赏
  • 举报
回复
那我该如何实现每执行100次,输出下执行时间呢
oh_Maxy 2019-09-08
  • 打赏
  • 举报
回复
这样会导致你所有的线程,公用一个runable对象,这是你期望的吗?
一般我们都是一个线程给一个runable对象。
Runnable ra = new test();这行移动到for循环里。

62,615

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧