该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
//program ScreenProtect - 2007.11.1 at night -THU-ZHANGLIANG && LIFENG
import java.awt.*;
import java.applet.Applet;
import java.util.*;
public class ScreenProtect extends Applet{
RunThread[] changeThread = new RunThread[20]; //define the circle number is 20
int[][] desk = new int[changeThread.length][6];//record the random circle's attributions
int threadnum; //recorde the current thread's number
public synchronized void start(){
for(int i = 0; i
for( int j = 0;j
desk[i][j] = 0;
for(int i = 0;i
changeThread[i] = new RunThread( i,"changeThread" );
changeThread[i].start(); //current thread call run()
}
}
public void paint( Graphics g ){ //paint a circle
int red = (int)(Math.random()*250);
int green = (int)(Math.random()*250);
int blue = (int)(Math.random()*250);
desk[threadnum][3] = red;
desk[threadnum][4] = green;
desk[threadnum][5] = blue;
if( desk[threadnum][2] == 0 ){
int xPos = (int)(Math.random()*900);
int yPos = (int)(Math.random()*650);
desk[threadnum][0] = xPos;
desk[threadnum][1] = yPos;
desk[threadnum][2] = 1; //circle's radius
}
else{
desk[threadnum][2]++;
if( desk[threadnum][2] == 200 ){
desk[threadnum][2] = 0;
}
}
for( int i = 0; i
g.setColor( new Color(desk[threadnum][3],desk[threadnum][4],desk[threadnum][5]));
g.fillOval( desk[threadnum][0],desk[threadnum][1],desk[threadnum][2],desk[threadnum][2] );
}
}
class RunThread extends Thread{ //define thread class
int num;
String threadName;
public RunThread( int num ,String str ){
this.num = num;
threadName = str;
}
public synchronized void run(){
while( true ){
try{
synchronized (this){ //marker the synchronized keywords
threadnum = num;
Thread.sleep(100);
repaint();
}
}
catch( Exception e ){
}
}
}
}
}
这是我刚开始学java的时候和同学一起写的,好像线程同步没处理好,也一直没没去完善,先看看吧,可能也没什么价�