多线程并行的问题 我对java的多线程不是很熟悉,找了很久也没找到合适的例子.我想实现这么个功能,在List里装有待处理的对象,用多个线程从List里取得对象并处理,把读取过的对象从List里删去..在操作list的时候加锁...我自己试着写了个可是不象并行运行的,有谁能给我个例子么?不胜感激... 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 加锁只是在读list的时候加啊,读list占的时间很少吧..主要是为了并行花费时间长的处理.我自己瞎写的一个,完全不能用...能告诉我下是什么地方出问题了么?我现在在请教了别人以后会通过给list操作加synchronized来实现了..但是还是想知道我这个程序是什么问题.package threadtest;import java.util.ArrayList;import java.util.List;public class Threadtestold implements Runnable{ private List<String> sourceList; private List<String> targetList=new ArrayList(); private boolean isReading; private boolean isWriting; private String atom; public Threadtestold(int n,List list,boolean is){ this.sourceList=list; this.isReading=is; } public synchronized void read(){ if(isReading){ try{ wait(); }catch(InterruptedException e){ } }else{ String ThreadName=Thread.currentThread().getName(); isReading=true; long timeStart = System.currentTimeMillis();//test long time=0; if(sourceList.size()!=0){ atom=sourceList.get(0); sourceList.remove(0); isReading=false; } notify(); while(time>-1){ long timeEnd = System.currentTimeMillis();//test time=(timeStart-timeEnd)/1000; } System.out.println(ThreadName+" is reading"+"the string is: "+atom); } } public void run(){ while(sourceList.size()!=0){ read(); } }} }else{ String ThreadName=Thread.currentThread().getName(); isReading=true; long timeStart = System.currentTimeMillis();//test long time=0; if(sourceList.size()!=0){ atom=sourceList.get(0); sourceList.remove(0); isReading=false; } notify(); while(time>-1){ long timeEnd = System.currentTimeMillis();//test time=(timeStart-timeEnd)/1000; 代码贴错了吧?怎么run()方法后面完全不懂了 public synchronized void read()这说明同时只能有一个线程在执行这个函数 public synchronized void read()这说明同时只能有一个线程在执行这个函数 你还在函数利用了wait????? 你干脆用vector不就得了,直接不用考虑多线程得问题 确实不懂你的意图....好像用不着wait(),notify()吧再说构造每个Threadtestold对象的参数List是不是同一个啊 List 有一个 remove 方法! 你对这个方法加锁就可以了!像这样 public synchronized Object getItem(java.util.List list) { Object obj = null; if (list.size() <= 0) return obj; else return list.remove(0); } public class Factory { private static Factory factory; private static Object classLock = Factory.class; private Factory() {} public static Factory getFactory() { synchronized (classLock) { if ( factory == null ) factory = new Factory(); return factory; } }}LZ说的应该是同步的问题以上代码,防止多个线程同时调用getFactory()方法仅供参考 我大概搞明白了.但是还有些问题..多线程的时候是比如我的类叫public class ThreadTest extends Thread{}那么每开一个线程是都要用Thread t=new ThreadTest()么,每个线程都是一个该类的实例?那比如我要多线程处理一些数据,然后最后我把这数据取出来是只能用引用把这数据传进去么?能不能做个get方法一类的?怎么能知道ThreadTest这个类生成的所有线程是不是都结束了? System.in 的in是InputStream. 可是InputStream是抽象类。。。。(20分钟后结贴) 关于数组的问题 好奇怪!好郁闷啊!JMenu的下拉菜单弹不出来 大家给点意见(会影响我的人生 关于数据库的问题,求解 急!!js引入问题 用尽的ResultSet 是怎么回事 tomcat3.3和sqlserver2K结合当并发访问的时候,发生操作阻塞现象!! 这里人气太低了,怎么搞的 纯java能做服务器端的SESSION保存提交信息吗? 插入多条数据效率问题 如何获得文件系统信息?
import java.util.List;public class Threadtestold implements Runnable{
private List<String> sourceList;
private List<String> targetList=new ArrayList();
private boolean isReading;
private boolean isWriting;
private String atom;
public Threadtestold(int n,List list,boolean is){
this.sourceList=list;
this.isReading=is;
}
public synchronized void read(){
if(isReading){
try{
wait();
}catch(InterruptedException e){
}
}else{
String ThreadName=Thread.currentThread().getName();
isReading=true;
long timeStart = System.currentTimeMillis();//test
long time=0;
if(sourceList.size()!=0){
atom=sourceList.get(0);
sourceList.remove(0);
isReading=false;
} notify();
while(time>-1){
long timeEnd = System.currentTimeMillis();//test
time=(timeStart-timeEnd)/1000;
}
System.out.println(ThreadName+" is reading"+"the string is: "+atom); }
}
public void run(){
while(sourceList.size()!=0){
read();
}
}
}
String ThreadName=Thread.currentThread().getName();
isReading=true;
long timeStart = System.currentTimeMillis();//test
long time=0;
if(sourceList.size()!=0){
atom=sourceList.get(0);
sourceList.remove(0);
isReading=false;
} notify();
while(time>-1){
long timeEnd = System.currentTimeMillis();//test
time=(timeStart-timeEnd)/1000;
你还在函数利用了wait?????
再说构造每个Threadtestold对象的参数List是不是同一个啊
Object obj = null;
if (list.size() <= 0)
return obj;
else
return list.remove(0);
}
public class Factory {
private static Factory factory;
private static Object classLock = Factory.class; private Factory() {}
public static Factory getFactory() {
synchronized (classLock) {
if ( factory == null )
factory = new Factory(); return factory;
}
}
}LZ说的应该是同步的问题
以上代码,防止多个线程同时调用getFactory()方法
仅供参考
那么每开一个线程是都要用Thread t=new ThreadTest()么,每个线程都是一个该类的实例?
那比如我要多线程处理一些数据,然后最后我把这数据取出来是只能用引用把这数据传进去么?能不能做个get方法一类的?怎么能知道ThreadTest这个类生成的所有线程是不是都结束了?