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
相关文档:
一:准备 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反射机制得到类的结构信息
关键字: java反射 类的结构
使用Java反射机制得到类的结构信息
代码如下:
Java代码
package com.youdao.wm;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import jav ......
-------------------------------------------------
本教程由yyc,spirit整理
------------------------------------------------- 第6章 类再生
“Java引人注目的一项特性是代码的重复使用或者再生。但最具革命意义的是,除代码的复制和修改以外,我们还能做多得多的其他事情。”
在象C那样的 ......
StringBuffer也是字符串,与String不同的是StringBuffer对象创建完之后可以修改内容。有如下构造函数:
n public StringBuffer(int);
n public StringBuffer(String);
n ......