java在for循环中使用concurrent包进行多线程编程
最近在做接口的时候总是遇到一个for语句中 每次循环会涉及很多资源,包括 ftp io db,总想用现场来控制太.找到一篇文章 http://daoger.javaeye.com/blog/142485 写的不错.自己写了2个demo
1. 主线程不等待
public class CopyOfTestThreadPool {
public static void main(String args[]) throws InterruptedException {
// only two threads
ExecutorService exec = Executors.newFixedThreadPool(20);
List<Long> list = new ArrayList<Long>();
for(int index = 0; index < 1000000; index++){
list.add(System.nanoTime());
}
long start = System.currentTimeMillis();
for (Long long1 : list) {
final Long l = long1;
exec.execute(new Runnable(){
public void run() {
System.out.println(l);
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}});
}
// must shutdown
exec.shutdown();
long end = System.currentTimeMillis();
System.out.print("共计用时 ");
System.out.println(end - start);
}
}
2 主线程会等待
public class TestCountDownLatch {
public static void main(String[] args) throws InterruptedException {
long start = System.currentTimeMillis();
// 开始的倒数锁
final CountDownLatch begin = new CountDownLatch(1);
// 结束的倒数锁
final CountDownLatch end = new CountDownLatch(10000);
// 十名选手
final ExecutorService exec = Executors.newFixedThrea
相关文档:
Java学习从入门到精通
一、 JDK (Java Development Kit)
JDK是整个Java的核心,包括了Java运行环境(Java Runtime Envirnment),一堆Java工具和Java基础的类库(rt.jar)。不论什么Java应用服务器实质都是内置了某个版本的JDK。因此掌握JDK是学好Java的第一步。最主流的J ......
一:准备 www.savarese.org download
1. rocksaw-1.0.0-src.tar.gz
2. vserv-tcpip-0.9.2-src.tar.gz
二:编译源文件得到jar包 使用Ant
1. build vserv-tcpip-0.9.2-src
在vserv-tcpip-0.9.2目录下面建一个tests目录,然后在cmd窗口下进入 ......
package io;
import java.io.*;
/**
* @author 高枕吴忧
* 利用缓冲区原理,BufferedInputStream,
* 实现的文件字节流读取功能示范
*
*/
public class BufferedInOutputStream {
public BufferedInOutputStream() {
ioTest2();
}
public void ioTest2() {
FileInputStream in = null ;
Buffered ......
java中的反射机制通过invoke方法来实现,这中间主要是是通过一下几个函数实现的
Class.forName ("类名")
Class.forName ("类名").newInstance
Class.getMethod ("调用的方法名",class[]{参数类列表})
instance.getClass
Method.invoke ("对象名","参数封装成的对象")
具体怎么实现,让我们来看一个简单的例子: ......