java多线程示例

<div class="iteye-blog-content-contain" style="font-size: 14px"></div>


Java 多线程实例-测试多线程效率

部分代码来自网络
欢迎加入Q-Q群:123866420,加入请说明:来自iteye.com
代码在附件中,方便阅读请下载 写最好的文章 留个赞哦

创建一个固定大小的txt文件

代码如下:
package test.multiThread;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

publicclass FileCreate {
privatefinalstatic String path = "C:\\Users\\Administrator\\Desktop\\test1.txt";
privatefinalstatic String crlf=System.getProperty("line.separator");
//单位是kb
privatefinalstaticintsize = 1024*5; //5兆
//是否限制文件大小
privatestaticbooleanpsize = true;
publicstaticvoid main(String[] args) throws IOException {
File file = null;
OutputStream out = null;
if(psize){
file = new File(path);
int i=0;
while(psize && file.length()<= size*1024){
out = new FileOutputStream(file,true);
out = new FileOutputStream(file,true);
out.write((String.valueOf(i)+"------").getBytes());
out.write(crlf.getBytes());
i++;
}
}else{
file = new File(path);
for (int i = 0; i <= 1500; i++) {
out = new FileOutputStream(file,true);
out.write((String.valueOf(i)+"------").getBytes());
out.write(crlf.getBytes());
}
}
if(null != out){
out.flush();
out.close();
}
file = null;
out = null;

}
}




读取文件方法体,继承Runnable实现多线程

代码如下:
package test.multiThread;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

class MultiThread implements Runnable{
private static BufferedReader br = null;
private List<String> list;

static{
try {
br = new BufferedReader(new FileReader("C:\\Users\\Administrator\\Desktop\\test1.txt"),3);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}

public void run() {
String line = null;
int count = 0;
while(true) {
this.list = new ArrayList<String>();
synchronized(br) {
try {
while((line = br.readLine()) != null) {
if(count<3) {
list.add(line+Thread.currentThread().getName());
count++;
}else {
list.add(line+Thread.currentThread().getName());
count = 0;
break;
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
try {
Thread.sleep(1);
display(this.list);
} catch (InterruptedException e) {
e.printStackTrace();
}
if(line == null)
break;
}


}
/**
* 打印读取的内容,不建议打印,影响效率
*/
public void display(List<String> list) {
for(String str:list) {
System.out.println(str);
}
//System.out.println(list.size());
}

}

测试多线程效率
代码如下:
package test.multiThread;

import java.util.Date;

/** @author zongcai_liu
* @email 892530175@qq.com
* 2014-3-12 下午3:36:38
*/
publicclass ThreadReadDemo {
publicstaticvoid main(String[] args) throws InterruptedException {
Date now = new Date();
//线程数目
int threadNum = 10;
Thread[] thread = new Thread[threadNum];
for(int i=0;i<thread.length;i++){
thread[i] = new Thread(new MultiThread(),String.valueOf(i));
thread[i].start();
//thread[i].join();
}
for(int i=0;i<thread.length;i++){
while(thread[i].isAlive()){
}
}
Date end = new Date();
System.out.println("------线程时间---------");
System.out.println((end.getTime()-now.getTime())/1000+"秒");
}
}













测试结果:

5M的txt文件

结果1:开启1个线程读取时间:
第一次:
<!--[if gte vml 1]><v:shapetype id="_x0000_t75"
coordsize="21600,21600" o:spt="75" o:preferrelative="t" path="m@4@5l@4@11@9@11@9@5xe"
filled="f" stroked="f">
<v:stroke joinstyle="miter"/>
<v:formulas>
<v:f eqn="if lineDrawn pixelLineWidth 0"/>
<v:f eqn="sum @0 1 0"/>
<v:f eqn="sum 0 0 @1"/>
<v:f eqn="prod @2 1 2"/>
<v:f eqn="prod @3 21600 pixelWidth"/>
<v:f eqn="prod @3 21600 pixelHeight"/>
<v:f eqn="sum @0 0 1"/>
<v:f eqn="prod @6 1 2"/>
<v:f eqn="prod @7 21600 pixelWidth"/>
<v:f eqn="sum @8 21600 0"/>
<v:f eqn="prod @7 21600 pixelHeight"/>
<v:f eqn="sum @10 21600 0"/>
</v:formulas>
<v:path o:extrusionok="f" gradientshapeok="t" o:connecttype="rect"/>
<o:lock v:ext="edit" aspectratio="t"/>
</v:shapetype><v:shape id="图片_x0020_1" o:spid="_x0000_i1032" type="#_x0000_t75"
alt="说明: C:\Users\Administrator\AppData\Roaming\Tencent\Users\892530175\QQ\WinTemp\RichOle\9AR($ZIR976%ISI39FN8I86.jpg"
style='width:157.5pt;height:32.25pt;visibility:visible;mso-wrap-style:square'>
<v:imagedata src="file:///C:\Users\ADMINI~1\AppData\Local\Temp\msohtmlclip1\01\clip_image001.jpg"
o:title="9AR($ZIR976%ISI39FN8I86"/>
</v:shape><![endif]--><!--[if !vml]--><!--[endif]-->
第2次:
<!--[if gte vml 1]><v:shape
id="图片_x0020_2" o:spid="_x0000_i1031" type="#_x0000_t75" style='width:152.25pt;
height:45pt;visibility:visible;mso-wrap-style:square'>
<v:imagedata src="file:///C:\Users\ADMINI~1\AppData\Local\Temp\msohtmlclip1\01\clip_image002.png"
o:title=""/>
</v:shape><![endif]--><!--[if !vml]--><!--[endif]-->


结果2:开启5个线程读取时间:
第一次:
<!--[if gte vml 1]><v:shape id="图片_x0020_5"
o:spid="_x0000_i1030" type="#_x0000_t75" style='width:167.25pt;height:51pt;
visibility:visible;mso-wrap-style:square'>
<v:imagedata src="file:///C:\Users\ADMINI~1\AppData\Local\Temp\msohtmlclip1\01\clip_image003.png"
o:title=""/>
</v:shape><![endif]--><!--[if !vml]--><!--[endif]-->
第2次:
<!--[if gte vml 1]><v:shape
id="图片_x0020_6" o:spid="_x0000_i1029" type="#_x0000_t75" style='width:169.5pt;
height:52.5pt;visibility:visible;mso-wrap-style:square'>
<v:imagedata src="file:///C:\Users\ADMINI~1\AppData\Local\Temp\msohtmlclip1\01\clip_image004.png"
o:title=""/>
</v:shape><![endif]--><!--[if !vml]--><!--[endif]-->

结果3:开启10个线程读取时间:
第一次:
<!--[if gte vml 1]><v:shape id="图片_x0020_9"
o:spid="_x0000_i1028" type="#_x0000_t75" style='width:150.75pt;height:45pt;
visibility:visible;mso-wrap-style:square'>
<v:imagedata src="file:///C:\Users\ADMINI~1\AppData\Local\Temp\msohtmlclip1\01\clip_image005.png"
o:title=""/>
</v:shape><![endif]--><!--[if !vml]--><!--[endif]-->
第2次:
<!--[if gte vml 1]><v:shape
id="图片_x0020_10" o:spid="_x0000_i1027" type="#_x0000_t75" style='width:204.75pt;
height:56.25pt;visibility:visible;mso-wrap-style:square'>
<v:imagedata src="file:///C:\Users\ADMINI~1\AppData\Local\Temp\msohtmlclip1\01\clip_image006.png"
o:title=""/>
</v:shape><![endif]--><!--[if !vml]--><!--[endif]-->

结果3:开启50个线程读取时间:
第一次:
<!--[if gte vml 1]><v:shape id="图片_x0020_13"
o:spid="_x0000_i1026" type="#_x0000_t75" style='width:192pt;height:62.25pt;
visibility:visible;mso-wrap-style:square'>
<v:imagedata src="file:///C:\Users\ADMINI~1\AppData\Local\Temp\msohtmlclip1\01\clip_image007.png"
o:title=""/>
</v:shape><![endif]--><!--[if !vml]--><!--[endif]-->
第2次:
<!--[if gte vml 1]><v:shape
id="图片_x0020_14" o:spid="_x0000_i1025" type="#_x0000_t75" style='width:179.25pt;
height:93.75pt;visibility:visible;mso-wrap-style:square'>
<v:imagedata src="file:///C:\Users\ADMINI~1\AppData\Local\Temp\msohtmlclip1\01\clip_image008.png"
o:title=""/>
</v:shape><![endif]--><!--[if !vml]--><!--[endif]-->
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值