求高人就我:
我的线程代码如下:
每次启动后,工作一个不确定的时间后(运行100多次,或者10几次)就会自己死掉. 我实在找不出问题了.
public class mapSetup extends Thread {
private ArrayList<Shape> shape;
private int[][][] vertex;
private Canvas mc;
private static int isx=0;
/** Creates a new instance of mapSetup */
public mapSetup(Canvas C ,ArrayList<Shape> sa ) {
shape= sa;
mc=C;
vertex = initVertex();
this.setDaemon(true);
}
public mapSetup(int[][][] v ) {
vertex = v;
}
/*************** methord for function **************/
public void run() {
int[] yy={110,110};
int count =40;
try{
while(true){
if (count>19) {
yy =readVertex();
calculateVertex(yy , vertex);
System.out.println(this.getClass()+"[y1="
+ yy[0]+"],[y2 =" +yy[1]+"] | count = " + count);
count=0;
}
else {
moveVertex(vertex);
count ++;
}
showVertex(vertex);
// buildPolygon(vertex,shape);
// mc.repaint();
sleep(40);
}
}catch (InterruptedException inex) {
System.err.println(this.getClass() +"sleep failed.");}
}
private int[] readVertex(){
int[] yy =new int[2];
Random rand = new Random();
yy[0]=rand.nextInt(200);
while((yy[1]=yy[0]+80+rand.nextInt(160))>240);
return yy;
}
/**
*shapCalculator(int[] y,int[] polygon)
*Calculate vertex of polygon
*int[][][] polyVertex , array for polygon.
*polyVertex [up/down 2 ][x/y 2][element 12]
*polyVertex 12 pionts
*/
private synchronized void calculateVertex(int[] y , int[][][] polyVertex){
// int[][][] polyVertex =new int[2][2][12];
//for down polygon
for(int i = 0 ; i < 9 ; i++ ) {
//move y
polyVertex[1][1][i]=polyVertex[1][1][i+1];
}
//polyVertex[][1][] -- --- array of y
polyVertex[1][1][9] =y[1];
//fill x value
for(int i =0 ; i<10;i++)
polyVertex[1][0][i]= i * 40;
polyVertex[1][0][10]=360;
polyVertex[1][1][10]=240;
polyVertex[1][0][11]=0;
polyVertex[1][1][11]=240;
//for up polygon
for(int i = 11 ; i >2 ; i--)
polyVertex[0][1][i]=polyVertex[0][1][i-1];
//polyVertex[][1][] -- --- array of y
polyVertex[0][1][2] =y[0];
//fill x value
for(int i =2 ; i<12;i++)
polyVertex[0][0][i]= 360-(i-2) * 40;
polyVertex[0][1][0]=0;
polyVertex[0][0][0]=0;
polyVertex[0][1][1]=0;
polyVertex[0][0][1]=360;
// showVertex(polyVertex);
}
private synchronized void moveVertex(int[][][] vertexA) {
for(int i =2;i<12;i++)
vertexA[0][0][i] -=2;
for(int i =0;i<9;i++)
vertexA[1][0][i] -=2;
}
private synchronized void showVertex(int[][][] polyVertex){
System.out.println("-------polyVertex ------" + isx++);
for (int i =0; i<12;i++) {
System.out.println("upP[" +i+"] ( "+polyVertex[0][0][i]","+polyVertex[0][1][i] +") ,downP[" +i+"] ( "+polyVertex[1][0][i]+","+polyVertex[1][1][i] +") ");
} }
private synchronized void buildPolygon( int[][][] polyVertex ,ArrayList ssa) {
ssa.clear();
ssa.add(new Polygon(polyVertex[0][0],polyVertex[0][1],12));
ssa.add(new Polygon(polyVertex[1][0],polyVertex[1][1],12));
}
public static int[][][] initVertex(){
int[][][] polyVertex =new int[2][2][12];
Random rand =new Random();
//for down polygon
for(int i = 0 ; i < 9 ; i++ ) {
//move y
polyVertex[1][1][i]=rand.nextInt(200);
}
//polyVertex[][1][] -- --- array of y
polyVertex[1][1][9] =100;
//fill x value
for(int i =0 ; i<10;i++) {
polyVertex[1][0][i]= i * 40;
}
polyVertex[1][0][10]=360;
polyVertex[1][1][10]=240;
polyVertex[1][0][11]=0;
polyVertex[1][1][11]=240;
//for up polygon
for(int i = 11 ; i >2 ; i--) {
polyVertex[0][1][i]=rand.nextInt(200);;
}
//polyVertex[][1][] -- --- array of y
polyVertex[0][1][2] =100;
//fill x value
for(int i =2 ; i<12;i++) {
polyVertex[0][0][i]= 360-(i-2) * 40;
}
polyVertex[0][1][0]=0;
polyVertex[0][0][0]=0;
polyVertex[0][1][1]=0;
polyVertex[0][0][1]=360;
return polyVertex;
}
}测试代码如下:
public class testMap {
public static void main(String[] args) {
int[][][] vert = mapSetup.initVertex();
new mapSetup(vert).start();
}
}
我的线程代码如下:
每次启动后,工作一个不确定的时间后(运行100多次,或者10几次)就会自己死掉. 我实在找不出问题了.
public class mapSetup extends Thread {
private ArrayList<Shape> shape;
private int[][][] vertex;
private Canvas mc;
private static int isx=0;
/** Creates a new instance of mapSetup */
public mapSetup(Canvas C ,ArrayList<Shape> sa ) {
shape= sa;
mc=C;
vertex = initVertex();
this.setDaemon(true);
}
public mapSetup(int[][][] v ) {
vertex = v;
}
/*************** methord for function **************/
public void run() {
int[] yy={110,110};
int count =40;
try{
while(true){
if (count>19) {
yy =readVertex();
calculateVertex(yy , vertex);
System.out.println(this.getClass()+"[y1="
+ yy[0]+"],[y2 =" +yy[1]+"] | count = " + count);
count=0;
}
else {
moveVertex(vertex);
count ++;
}
showVertex(vertex);
// buildPolygon(vertex,shape);
// mc.repaint();
sleep(40);
}
}catch (InterruptedException inex) {
System.err.println(this.getClass() +"sleep failed.");}
}
private int[] readVertex(){
int[] yy =new int[2];
Random rand = new Random();
yy[0]=rand.nextInt(200);
while((yy[1]=yy[0]+80+rand.nextInt(160))>240);
return yy;
}
/**
*shapCalculator(int[] y,int[] polygon)
*Calculate vertex of polygon
*int[][][] polyVertex , array for polygon.
*polyVertex [up/down 2 ][x/y 2][element 12]
*polyVertex 12 pionts
*/
private synchronized void calculateVertex(int[] y , int[][][] polyVertex){
// int[][][] polyVertex =new int[2][2][12];
//for down polygon
for(int i = 0 ; i < 9 ; i++ ) {
//move y
polyVertex[1][1][i]=polyVertex[1][1][i+1];
}
//polyVertex[][1][] -- --- array of y
polyVertex[1][1][9] =y[1];
//fill x value
for(int i =0 ; i<10;i++)
polyVertex[1][0][i]= i * 40;
polyVertex[1][0][10]=360;
polyVertex[1][1][10]=240;
polyVertex[1][0][11]=0;
polyVertex[1][1][11]=240;
//for up polygon
for(int i = 11 ; i >2 ; i--)
polyVertex[0][1][i]=polyVertex[0][1][i-1];
//polyVertex[][1][] -- --- array of y
polyVertex[0][1][2] =y[0];
//fill x value
for(int i =2 ; i<12;i++)
polyVertex[0][0][i]= 360-(i-2) * 40;
polyVertex[0][1][0]=0;
polyVertex[0][0][0]=0;
polyVertex[0][1][1]=0;
polyVertex[0][0][1]=360;
// showVertex(polyVertex);
}
private synchronized void moveVertex(int[][][] vertexA) {
for(int i =2;i<12;i++)
vertexA[0][0][i] -=2;
for(int i =0;i<9;i++)
vertexA[1][0][i] -=2;
}
private synchronized void showVertex(int[][][] polyVertex){
System.out.println("-------polyVertex ------" + isx++);
for (int i =0; i<12;i++) {
System.out.println("upP[" +i+"] ( "+polyVertex[0][0][i]","+polyVertex[0][1][i] +") ,downP[" +i+"] ( "+polyVertex[1][0][i]+","+polyVertex[1][1][i] +") ");
} }
private synchronized void buildPolygon( int[][][] polyVertex ,ArrayList ssa) {
ssa.clear();
ssa.add(new Polygon(polyVertex[0][0],polyVertex[0][1],12));
ssa.add(new Polygon(polyVertex[1][0],polyVertex[1][1],12));
}
public static int[][][] initVertex(){
int[][][] polyVertex =new int[2][2][12];
Random rand =new Random();
//for down polygon
for(int i = 0 ; i < 9 ; i++ ) {
//move y
polyVertex[1][1][i]=rand.nextInt(200);
}
//polyVertex[][1][] -- --- array of y
polyVertex[1][1][9] =100;
//fill x value
for(int i =0 ; i<10;i++) {
polyVertex[1][0][i]= i * 40;
}
polyVertex[1][0][10]=360;
polyVertex[1][1][10]=240;
polyVertex[1][0][11]=0;
polyVertex[1][1][11]=240;
//for up polygon
for(int i = 11 ; i >2 ; i--) {
polyVertex[0][1][i]=rand.nextInt(200);;
}
//polyVertex[][1][] -- --- array of y
polyVertex[0][1][2] =100;
//fill x value
for(int i =2 ; i<12;i++) {
polyVertex[0][0][i]= 360-(i-2) * 40;
}
polyVertex[0][1][0]=0;
polyVertex[0][0][0]=0;
polyVertex[0][1][1]=0;
polyVertex[0][0][1]=360;
return polyVertex;
}
}测试代码如下:
public class testMap {
public static void main(String[] args) {
int[][][] vert = mapSetup.initVertex();
new mapSetup(vert).start();
}
}
int[] yy =new int[2];
Random rand = new Random();
yy[0]=rand.nextInt(200);
while((yy[1]=yy[0]+80+rand.nextInt(160))>240);
return yy;
}
就是这个函数进入了死循环。当yy[0]=rand.nextInt(200);大于等于160时,while((yy[1]=yy[0]+80+rand.nextInt(160))>240); 永远不会退出。