顶一下,拜托各位了,我已经弄了好几天了。修改成多线程,编译通过,为什么运行的时候出现ArrayIndexOutOfBoundsException异常??
package flare;
import java.lang.*;
import java.util.*;
import java.text.*;
import java.applet.*;public class select extends Thread
{
Vector ve = new Vector();
Vector veSplus = new Vector();
public void run()//代入种群大小
{
Thread thread = new Thread();
int i;
Random r = new Random();
double pick=Math.random();//[0,1)随机数
double sum=0;
oldfitness old=new oldfitness();
for (i=0;(sum<pick)&&(i<200);i++)
{
old.solutionFitness(200,20);
double tmp1=Double.parseDouble((String)old.veS.elementAt(2));
//if (tmp1!=0)
sum+=tmp1/Double.parseDouble((String)old.veS.elementAt(3));
//else
// popsize+=1;
//System.out.println("----------------"+sum+" "+pick+" "+i);
}
int tmp_i=i-1;
ve.addElement(Integer.toString(tmp_i));
for (int k=0;k<45;k++)
veSplus.addElement(old.veSplus.elementAt(k));
}
public static void main(String args[])
{
select se=new select();
for (int i=0;i<200;i++)//输出200个被选择得解个体,并输出45个权重
{
se.start();
for (int k=0;k<45;k++)
{
System.out.println(se.veSplus.elementAt(k)+"----------"+k);
}
System.out.println("被选中的解个体编号为:"+se.ve.elementAt(i));
}
}
//*/
}
package flare;
import java.lang.*;
import java.util.*;
import java.text.*;
import java.applet.*;public class select extends Thread
{
Vector ve = new Vector();
Vector veSplus = new Vector();
public void run()//代入种群大小
{
Thread thread = new Thread();
int i;
Random r = new Random();
double pick=Math.random();//[0,1)随机数
double sum=0;
oldfitness old=new oldfitness();
for (i=0;(sum<pick)&&(i<200);i++)
{
old.solutionFitness(200,20);
double tmp1=Double.parseDouble((String)old.veS.elementAt(2));
//if (tmp1!=0)
sum+=tmp1/Double.parseDouble((String)old.veS.elementAt(3));
//else
// popsize+=1;
//System.out.println("----------------"+sum+" "+pick+" "+i);
}
int tmp_i=i-1;
ve.addElement(Integer.toString(tmp_i));
for (int k=0;k<45;k++)
veSplus.addElement(old.veSplus.elementAt(k));
}
public static void main(String args[])
{
select se=new select();
for (int i=0;i<200;i++)//输出200个被选择得解个体,并输出45个权重
{
se.start();
for (int k=0;k<45;k++)
{
System.out.println(se.veSplus.elementAt(k)+"----------"+k);
}
System.out.println("被选中的解个体编号为:"+se.ve.elementAt(i));
}
}
//*/
}
for (int k=0;k<45;k++)
{
System.out.println(se.veSplus.elementAt(k)+"----------"+k);
}线程才开始,你就想读数据,当然有问题啦。数据还没有生成呢。
double sum=0;
sum+=tmp1/Double.parseDouble((String)old.veS.elementAt(3));
是什么意思?如果这里很关键的话,可以使用多线程就算——这是很好的切入点
-------------------------------------------------------------
我想把他锁住,就是当这个线程结束,结果出来时,在读数据,这该怎么改???-------------------------------------------------------------
sum+=tmp1/Double.parseDouble((String)old.veS.elementAt(3));
求出sum的值,并和一个随机数比较,如果大于,则得出i这个序号,然后把这个序号的45个变量拿出来(for(0..45)那里),因为我不知道该怎么把这个序号了这45个变量连起来,加入一个容器中,所以我只能用这种傻办法
45个变量是double型
for (i=0;(sum<pick)&&(i<200);i++) {
//System.out.println("Before Calling solutionFitness " + System.currentTimeMillis());
old.solutionFitness(200,20);
//System.out.println("End Calling solutionFitness " + System.currentTimeMillis());
double tmp1=Double.parseDouble((String)old.veS.elementAt(2));
//if (tmp1!=0)
sum+=tmp1/Double.parseDouble((String)old.veS.elementAt(3));
//else
// popsize+=1;
//System.out.println("----------------"+sum+" "+pick+" "+i);
}
System.out.println("end for loop " + System.currentTimeMillis()); 每做一次循环需要600以上。
//原则:尽量是用基本类型,高级的api是以牺牲性能为代价的。
package flare;import java.util.*;
import java.io.*;
import java.text.*;
public class RandomSolution
{
double vin[];
private Random r;
public RandomSolution() {
vin = new double[45];
r = new Random();
}
public void addin(){
//the following code just waste time. comment it!
//randomSolution ra = new randomSolution();
for (int i=0;i<45;i++)
{
vin[i] = this.randomRealNum();
}
}
public double randomRealNum(){
double s;
double plus_or_negative = r.nextInt(1000);
double tmp1 = (r.nextInt() % ( 1 * 1000 + 1) ) / 1000;
DecimalFormat df = new DecimalFormat("#0.00");
double rand = Double.parseDouble(df.format(tmp1,new StringBuffer(),new FieldPosition(0)).toString());
if (plus_or_negative>500)
{
s = -1*(rand);
}else{
s = 1*(rand);
}
return s;
}}
首先怎么判断所有的线程是否已经完成,完成后怎么拿所产生的数据?
能不能具体说说?TO takecare,之所以这么耗时,是因为前面那些类都是for,一层一层,所以才会得到一个结果这么久。
有没有什么解决的方法??我的想法就是,一个线程执行完后,把他的结果记录在Vector中,但是怎么判断这个线程是否完成,然后怎么把结果加入到Vector中呢?
import java.util.*;
import java.text.*;public class RandomTest
{
//Vector vin = new Vector();
double vin[];
private Random r;
DecimalFormat df;
public RandomTest () {
r = new Random();
vin = new double[200];
df = new DecimalFormat("#0.0");
}
public void testin(){
//the following line is just waste time!!!!!
//randomTest t = new randomTest();
for (int i=0;i<200;i++)
{
double aaa = ((double)r.nextInt() % ( 1 * 1000 + 1) ) / 1000;
//double aaa = (r.nextInt() % ( 1 * Math.pow(10,3) + 1) ) / Math.pow(10,3);
//vin.addElement(test);
vin[i] = Double.parseDouble(test);
//System.out.println((double)aaa);
}
}
public double test()
{
double d = r.nextInt(1000);
double s;
if (d>500)
{
s = 1;
}else{
s = -1;
}
return s;
}
public static void main (String argv[]) {
System.out.println(">>>>>>>>>" + System.currentTimeMillis());
RandomTest xTest = new RandomTest();
xTest.testin();
System.out.println("<<<<<<<<<" + System.currentTimeMillis());
}
}
我的想法就是,一个线程执行完后,把他的结果记录在Vector中,但是怎么判断这个线程是否完成,然后怎么把结果加入到Vector中呢?
这是可以分解为一个分布式计算的问题。判断线程是否结束可以用Thread的isAlive()的方法
,把结果加到Vector中可以采用共享内存的方法--需要同步。
其实是很简单的,自己看一下书吧
import java.io.*;
import java.text.*;
public class RandomSolution
{
double vin[];
private Random r;
public RandomSolution() {
vin = new double[45];
r = new Random();
}
public void addin(){
//the following code just waste time. comment it!
//randomSolution ra = new randomSolution();
for (int i=0;i<45;i++)
{
vin[i] = this.randomRealNum();
}
}
public double randomRealNum(){
double s;
double plus_or_negative = r.nextInt(1000);
double tmp1 = ((double)r.nextInt() % ( 1 * 1000 + 1) ) / 1000;
//DecimalFormat df = new DecimalFormat("#0.00");
//double rand = Double.parseDouble(df.format(tmp1,new StringBuffer(),new FieldPosition(0)).toString());
if (plus_or_negative>500)
{
s = -1*(tmp1);
}else{
s = 1*(tmp1);
}
return s;
}}
(r.nextInt() % ( 1 * 1000 + 1) ) / 1000;
改错了,我换回
(r.nextInt() % ( 1 * Math.pow(10,3) + 1) ) / Math.pow(10,3)
public double[] addin(){
//the following code just waste time. comment it!
//randomSolution ra = new randomSolution();
for (int i=0;i<45;i++)
{
vin[i] = this.randomRealNum();
}
return vin;
}
http://expert.csdn.net/Expert/topic/1800/1800572.xml?temp=.7207453
同样的错误,大家看看,为什么?
多谢
import java.util.*;
import java.io.*;
import java.text.*;
public class nervenet
{
//Vector vH = new Vector();
//double vH;
//Vector veSplus = new Vector();
/*
public void nervenet()
{
//vH = new double[15];
}
//*/
public double net(double x,double y)
{
sigmod si = new sigmod();
StringBuffer sb = new StringBuffer();
DecimalFormat df = new DecimalFormat("#0.0000");//为了以防越界,所以定义了小数点后有几位
FieldPosition fp = new FieldPosition(0);
double[] vH = new double[15];
randomSolution ran = new randomSolution();
ran.addin();
//Enumeration e = ran.vin.elements();
//这里怎么改???怎么把所算的45个权重拿出,供其他类使用???
//for (int k=0;k<45;k++)//把解个体45个权重放入Vector
// veSplus.addElement(ran.vin.elementAt(k));
//求和函数和sigmod函数部分
for (int i=0;i<15;i++)
{
int sp=0;sp++;
double su = x*ran.vin[i]+y*ran.vin[i+1];
double formatSu = Double.parseDouble(df.format(su,new StringBuffer(),new FieldPosition(0)).toString());
double H = si.s(formatSu);
//vH.addElement(Double.toString(H));
vH[i] = H;
}
double s,k=0;
//Enumeration eH = vH.elements();
//A=H1*W31+H2*W32……+H15*W45
for (int j=0;j<15;j++)
{
s = vH[j]*ran.vin[30+j];
k+=s;
}
//ran.vin.clear();
return k;
}
}
import java.util.*;
import java.text.*;
import java.applet.*;public class select extends Thread
{
private Integer m_writeLock = new Integer(0);
Vector ve;
Vector veSplus = new Vector(); //i don't know whether this vector should be global or not
//if you want to return value by this vector, please modify the constructor
public select (Vector ve /*, Vector veSplus */) {
this.ve = ve;
//this.veSplus = veSplus;
super("Select Thread");
}
public void run()//´úÈëÖÖȺ´óС
{
//Thread thread = new Thread();
int i;
Random r = new Random();
double pick=Math.random();//[0,1)Ëæ»úÊý
double sum=0;
oldfitness old=new oldfitness();
System.out.println("Before for loop " + System.currentTimeMillis());
for (i=0;(sum<pick)&&(i<200);i++)
{
//your code here....
}
System.out.println("end for loop " + System.currentTimeMillis());
int tmp_i=i-1;
ve.addElement(Integer.toString(tmp_i));
synchronized( m_readLock ) {
for (int k=0;k<45;k++) {
veSplus.addElement(old.veSplus.elementAt(k));
System.out.println(veSplus.elementAt(k)+"----------"+k);
}
}
System.out.println("±»Ñ¡ÖеĽâ¸öÌå±àºÅΪ£º"+ve.toString());
}
public static void main(String args[])
{
for (int i=0;i<200;i++)
{
select se=new select();
se.start();
se.join();
}
System.out.println("Main Thread Terminated!");
}
}