易截截图软件、单文件、免安装、纯绿色、仅160KB

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 I/O常用流示例

package io;
import java.io.*;
/**
* @author 高枕吴忧
* 利用缓冲区原理,BufferedInputStream,
* 实现的文件字节流读取功能示范
*
*/
public class BufferedInOutputStream {
public BufferedInOutputStream() {
ioTest2();
}
public void ioTest2() {
FileInputStream in = null ;
Buffered ......

Java调用外部程序

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
public class CmdTest {
    private static final long serialVersionUID = -2650474785662737262L;
    public static void main(String[] args) throws Exception {
  &n ......

think in java ch6

-------------------------------------------------
本教程由yyc,spirit整理
------------------------------------------------- 第6章 类再生
“Java引人注目的一项特性是代码的重复使用或者再生。但最具革命意义的是,除代码的复制和修改以外,我们还能做多得多的其他事情。”
在象C那样的 ......

JAVA字符编码

JAVA字符编码
今天终于把JAVA里一个比较头痛的问题——字符编码弄清晰了,所以写一篇文章来纪念一下,也为大家提供一点自己的心得。
众所周知,JAVA为了国际通用,用的是UNICODE来保存里面的字符。而UNICODE只是一个种字符集,字符的存储和表示要用到一定的字符编码格式,而与UNICODE对应的字符编码格式就是我 ......

Java基础:第二十三讲 面向对象概述

之前介绍了Java的语法基础,接下来介绍面向对象的相关思想和概念。
本部分包含的主要内容包括:
1、类与对象,主要介绍类和对象之间的关系。
2、现实世界中的对象与软件系统中的对象,介绍如何从现实世界中的对象抽象出软件系统中的对象。
3、软件系统中的对象和类之间的关系,如何根据软件对象抽象出类。
4、类的定义 ......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号