之前写了有关java线程的循环执行和结束,因为想制作成exe文件,想把执行的效果加到窗口上,所以就结合了JFrame和JPanel写了这个程序,这里直接贴出代码,在窗口上运行的效果下面有附图。
package thread;
import java.awt.Graphics;
import java.text.SimpleDateFormat;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JPanel;
/**
* 线程没完没了的循环
*
* @author FuJianyong
* 2015-6-16下午09:22:11
*/
public class EndlessLoop {
//是否结束线程的旗标
volatile boolean flag = true;
//控制是否输出数字
volatile boolean numIncrement = false;
class Endless extends Thread{
public void run(){
long startTime = System.currentTimeMillis();
writeString("线程开始正在生成随机数...");
Random random = new Random();
//随机数开关
int power = 0;
//计数
int i = 0;
while(flag){
power = random.nextInt(50);
writeString("while循环产生的随机数: " + power);
if(power == 19){
//结束线程
flag = false;
break;
}
for(;;){
power = random.nextInt(888);
if(power % 2 == 0 || power > 200){
//偶数则计数器增加
numIncrement = true;
}else{
//奇数则计数器停止增加
numIncrement = false;
}
if(numIncrement){
writeString("for循环产生的随机数是: " + power + " 计数器增加: " + ++i + " " + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(System.currentTimeMillis()));
try {
Thread.sleep(power * 2);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}else{
writeString("for循环产生的随机数是: " + power + " 计数器停止...");
break;
}
}
}
long endTime = System.currentTimeMillis();
writeString("线程结束等待垃圾回收...");
writeString("=====线程执行开始时间为: " + new SimpleDateFormat("yyyy 年 MM 月 dd 日 HH 时 mm 分 ss 秒").format(startTime));
writeString("=====线程执行结束时间为: " + new SimpleDateFormat("yyyy 年 MM 月 dd 日 HH 时 mm 分 ss 秒").format(endTime));
}
}
public void start(){
new Endless().start();
}
public static void main(String[] args) {
new EndlessLoop().start();
}
//重写构造方法
public EndlessLoop(){
JFrame frame = new JFrame("test");
frame.setLocation(500, 200);
frame.setSize(500, 400);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(draw);
}
Draw draw = new Draw();
String temp0 = "";
String temp1 = "";
String temp2 = "";
String temp3 = "";
String temp4 = "";
String temp5 = "";
String temp6 = "";
String temp7 = "";
String temp8 = "";
String temp9 = "";
String temp10 = "";
class Draw extends JPanel{
public void paint(Graphics g){
super.paintComponent(g);
g.drawString(temp0, 20, 20);
g.drawString(temp1, 20, 50);
g.drawString(temp2, 20, 80);
g.drawString(temp3, 20, 110);
g.drawString(temp4, 20, 140);
g.drawString(temp5, 20, 170);
g.drawString(temp6, 20, 200);
g.drawString(temp7, 20, 230);
g.drawString(temp8, 20, 260);
g.drawString(temp9, 20, 290);
g.drawString(temp10, 20, 320);
g.dispose();
}
}
int i = 0;
public void writeString(String str){
switch(i){
case 0:
temp0 = str;
break;
case 1:
temp1 = str;
break;
case 2:
temp2 = str;
break;
case 3:
temp3 = str;
break;
case 4:
temp4 = str;
break;
case 5:
temp5 = str;
break;
case 6:
temp6 = str;
break;
case 7:
temp7 = str;
break;
case 8:
temp8 = str;
break;
case 9:
temp9 = str;
break;
case 10:
temp10 = str;
break;
}
if(i>10){
temp1 = temp2;
temp2 = temp3;
temp3 = temp4;
temp4 = temp5;
temp5 = temp6;
temp6 = temp7;
temp7 = temp8;
temp8 = temp9;
temp9 = temp10;
temp10 = str;
}
i++;
draw.repaint();
}
}