继之前的线程循环加到窗口中运行
编程技术  /  houtizong 发布于 3年前   113
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 blocke.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();}}
请勿发布不友善或者负能量的内容。与人为善,比聪明更重要!
技术博客集 - 网站简介:
前后端技术:
后端基于Hyperf2.1框架开发,前端使用Bootstrap可视化布局系统生成
网站主要作用:
1.编程技术分享及讨论交流,内置聊天系统;
2.测试交流框架问题,比如:Hyperf、Laravel、TP、beego;
3.本站数据是基于大数据采集等爬虫技术为基础助力分享知识,如有侵权请发邮件到站长邮箱,站长会尽快处理;
4.站长邮箱:[email protected];
文章归档
文章标签
友情链接