关于多线程共享数据的一些疑惑
2015-06-18 15:39
337 查看
class DownLoadThread extends Thread{ int start; int end; DownLoadThread(int start,int end){ this.start = start; this.end = end; } @Override public void run() { while(start <= end){ System.out.println(Thread.currentThread().getName()+":"+start++); /*try { Thread.sleep(30); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); }*/ } } } public class TestDown { public static void main(String[] args) { int size = 100; int threadCount = 3; int blockSize = size / threadCount; for(int i=1;i<=threadCount;i++){ int startIndex = (i-1)*blockSize; int endIndex = i*blockSize-1; if(i == threadCount){ endIndex = size; } System.out.println(i+":"+startIndex+"-->"+endIndex); new DownLoadThread(startIndex,endIndex).start(); } } }
在主函数中用for循环创建三个线程,并传入构造函数的参数
这其中
DownLoadThread是<span style="font-family: Arial, Helvetica, sans-serif;">extends</span>继承了Thread <span style="font-family: Arial, Helvetica, sans-serif;">当然</span><span style="font-family: Arial, Helvetica, sans-serif;">implements</span><span style="font-family: Arial, Helvetica, sans-serif;">实现Runnable也是可以的</span>
<span style="font-family: Arial, Helvetica, sans-serif;">这里在重载run函数实现数据共享。</span>
但是有好多地方要使用线程,有没有想过为什么要使用他难道仅仅是一个共享数据这显然不对
弄个例子说明一下
点击打开链接
相关文章推荐
- java-模拟tomcat服务器
- Python3写爬虫(四)多线程实现数据爬取
- C#实现多线程的同步方法实例分析
- C#简单多线程同步和优先权用法实例
- C#多线程学习之(四)使用线程池进行多线程的自动管理
- C#多线程编程中的锁系统(三)
- C#多线程学习之(六)互斥对象用法实例
- 基于一个应用程序多线程误用的分析详解
- C#多线程学习之(三)生产者和消费者用法分析
- C#多线程学习之(一)多线程的相关概念分析
- C#多线程之Thread中Thread.IsAlive属性用法分析
- C#控制台下测试多线程的方法
- Ruby 多线程的潜力和弱点分析
- C#中WPF使用多线程调用窗体组件的方法
- c#实现多线程局域网聊天系统
- PHP使用CURL实现多线程抓取网页
- 浅解关于C#多线程的介绍
- 批处理程序中的“多线程”处理代码
- c#中多线程访问winform控件的若干问题小结
- linux网络编程----->高并发--->多线程并发服务器