论坛首页 Java企业应用论坛

Java多线程--让主线程等待所有子线程执行完毕

浏览 54570 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (1) :: 隐藏帖 (0)
作者 正文
   发表时间:2010-01-29  
nishizhutoua 写道
说句老实话,你确定你的这个需求要用多线程么?
读的方面,瓶颈在IO,用单线程,全速的读就是最快的了.除非你在读取一行数据后要对行数据进行格式化,并且这个格式化时间消耗要大于理论上的连续读两行文本时的CPU时间. 不过恐怕不容易吧,这两个时间差好几个级.
写的部分,瓶颈还是在IO,因为你要写到数据库中.
这么算两个线程就够了,生产者消费者模式即可.

假设有100万条记录,顺序执行的话光是遍历完这100万条记录就要很长时间了,现在开10个线程,每个线程只要插入10万条,是不是会快很多?
0 请登录后投票
   发表时间:2010-01-29  
yadsun 写道
nishizhutoua 写道
说句老实话,你确定你的这个需求要用多线程么?
读的方面,瓶颈在IO,用单线程,全速的读就是最快的了.除非你在读取一行数据后要对行数据进行格式化,并且这个格式化时间消耗要大于理论上的连续读两行文本时的CPU时间. 不过恐怕不容易吧,这两个时间差好几个级.
写的部分,瓶颈还是在IO,因为你要写到数据库中.
这么算两个线程就够了,生产者消费者模式即可.

假设有100万条记录,顺序执行的话光是遍历完这100万条记录就要很长时间了,现在开10个线程,每个线程只要插入10万条,是不是会快很多?

这个不一定,可能更慢。
并不是说开多个线程,速度就一定会快的,线程切换也有开销。
0 请登录后投票
   发表时间:2010-01-29  
多线程---批量线程同步模型
在一批线程处理程序中,有时必须等到所有线程全部运行完后,才能进行下一步任务处理,
可以采用如下方法解决,创建一个锁对象 ,该锁对象提供一个当前线程等待其他线程的方法。见代码:

/**  
 *   
 * 此类主要用来处理线程的同步屏蔽模型,比如,一批线程运行,必须在最后一个线程运行  
 * 完后,才能进行下一步的操作,那么就可以创建一个锁对象,锁对象提供一个线程等待其他线程  
 * 的方法,如果当前线程运行时,还有未运行的线程,则此线程wait,否则,此线程唤醒其他阻塞的  
 * 线程,进而最终完成线程的运行  
 * */  
public class LockObject {   
  
    private int totalThread = 0;   
    private int currentThread = 0;   
  
    public LockObject(int totalThread) {   
        this.totalThread = totalThread;   
        this.currentThread = 1;   
    }   
  
    public synchronized void waitForOtherThread() {   
        if (this.currentThread < this.totalThread) {   
            this.currentThread++;   
            try {   
                this.wait();   
            } catch (InterruptedException e) {   
                // TODO Auto-generated catch block   
                e.printStackTrace();   
            }   
        } else {   
            this.currentThread = 1;   
            notifyAll();   
        }   
    }   
  
    public int getTotalThread() {   
        return totalThread;   
    }   
  
    public void setTotalThread(int totalThread) {   
        this.totalThread = totalThread;   
    }   
  
    public int getCurrentThread() {   
        return currentThread;   
    }   
  
    public void setCurrentThread(int currentThread) {   
        this.currentThread = currentThread;   
    }   
}  

/**
 * 
 * 此类主要用来处理线程的同步屏蔽模型,比如,一批线程运行,必须在最后一个线程运行
 * 完后,才能进行下一步的操作,那么就可以创建一个锁对象,锁对象提供一个线程等待其他线程
 * 的方法,如果当前线程运行时,还有未运行的线程,则此线程wait,否则,此线程唤醒其他阻塞的
 * 线程,进而最终完成线程的运行
 * */
public class LockObject {

	private int totalThread = 0;
	private int currentThread = 0;

	public LockObject(int totalThread) {
		this.totalThread = totalThread;
		this.currentThread = 1;
	}

	public synchronized void waitForOtherThread() {
		if (this.currentThread < this.totalThread) {
			this.currentThread++;
			try {
				this.wait();
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} else {
			this.currentThread = 1;
			notifyAll();
		}
	}

	public int getTotalThread() {
		return totalThread;
	}

	public void setTotalThread(int totalThread) {
		this.totalThread = totalThread;
	}

	public int getCurrentThread() {
		return currentThread;
	}

	public void setCurrentThread(int currentThread) {
		this.currentThread = currentThread;
	}
}

此对象提供 二个私有变量,totalThread 的初始值为所运行的线程的总数,currentThread 为当前正在运行的线程数。 

此对象提供 二个私有变量,totalThread 的初始值为所运行的线程的总数,currentThread 为当前正在运行的线程数。Java代码
线程运行时处理完自己的任务后调用方法waitForOtherThread 等待其他线程结束,即当前运行线程数与线程总数的比较 

线程运行时处理完自己的任务后调用方法waitForOtherThread 等待其他线程结束,即当前运行线程数与线程总数的比较Java代码
如果运行线程数小于线程总数,则当前运行线程数+1 后,当前线程进入等待状态,否则,唤醒其他等待线程。 

如果运行线程数小于线程总数,则当前运行线程数+1 后,当前线程进入等待状态,否则,唤醒其他等待线程。
见测试程序

public class MyThread extends Thread {   
    public static LockObject lo = new LockObject(1000);   
  
    public MyThread(String threadName) {   
        super(threadName);   
    }   
  
    public void run() {   
            System.out.println(Thread.currentThread().getName() + " ----开始运行");   
            lo.waitForOtherThread();   
            System.out.println(Thread.currentThread().getName() + " ----结束运行");   
    }   
  
    public static void main(String[] args) {   
        for (int i = 1; i <= 1000; i++) {   
            Thread thread = new MyThread("第" + i + "个线程");   
            thread.setPriority(NORM_PRIORITY);   
            thread.start();   
        }   
    }   
  
}  

http://xuyan2680.iteye.com/admin/blogs/467701
0 请登录后投票
   发表时间:2010-01-29  
这个主题,如果是导入数据到Oracle,用SQL Loader最简单最快,如果在借题发挥到并发多任务,就是一个很大的话题了。

好像这篇帖子的内容相关
http://www.iteye.com/topic/405492
0 请登录后投票
   发表时间:2010-01-29  
大数据量,用oralce sqlLoad就行了,都不需要java
0 请登录后投票
   发表时间:2010-01-29  
楼主你的问题其实很简单。。。
countdown 就行了。。。

如果你确定了你开启的线程数。
那么在主程序运行前。
CountDownLatch countdown = new CountDownLatch(10);//这里的10就是你的子线程数。


在每个子线程结束后,调用 countdown.countDown();

在主线程里启动子线程的方法后面添加。
countdown.await();//这里进行同步等待

等所有子线程结束后,执行 countdown.await()后面的代码
0 请登录后投票
   发表时间:2010-01-30  
在main里面循环判断线程池里面线程是否运行完毕即可
0 请登录后投票
   发表时间:2010-01-31  
蓝皮鼠 写道
这个主题,如果是导入数据到Oracle,用SQL Loader最简单最快,如果在借题发挥到并发多任务,就是一个很大的话题了。

好像这篇帖子的内容相关
http://www.iteye.com/topic/405492

fuermos 写道
大数据量,用oralce sqlLoad就行了,都不需要java

因为表中有个字段是xmltype型的,插入时需调用oracle的sys.xmltype.createXml()来创建xmltype对象然后插入,这样服务器端压力会很大,所以想用java在客户端创建好XMLType对象,这样服务器端就少了创建xmltype对象的压力了
0 请登录后投票
   发表时间:2010-02-01  
ReadWriteLock rwl = new ReentrantReadWriteLock();

//执行子线程
for(....){
rwl.readLock().lock();
new Thread(new Runnable(){
  public void run(){
    try{
    ... 
   }finally{
    rwl.readLock().unlock();
   }
  }
}).start();
}

//等待子线程
try {
 rwl.writeLock().lock();
} finally {
 rwl.writeLock().unlock();
}
0 请登录后投票
   发表时间:2010-10-27  
把所有子线程启动了之后再逐一调用join()不行吗?
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics