java线程报时代码,线程的代码

java程序 时钟 整点报时代码

import java.awt.*;

站在用户的角度思考问题,与客户深入沟通,找到唐河网站设计与唐河网站推广的解决方案,凭借多年的经验,让设计与互联网技术结合,创造个性化、用户体验好的作品,建站类型包括:成都网站设计、做网站、企业官网、英文网站、手机端网站、网站推广、主机域名、网页空间、企业邮箱。业务覆盖唐河地区。

import java.util.*;

import java.awt.event.*;

import javax.swing.*;

import javax.swing.Timer;

public class Test extends JFrame implements ActionListener{

Timer t=new Timer(1000,this);

GregorianCalendar calendar=new GregorianCalendar();

JLabel label1=new JLabel(new Date().toString()),

label2=new JLabel("

");

public Test(){

super("TIME");

Container c=this.getContentPane();

c.setLayout(new FlowLayout());

c.add(label1);c.add(label2);

t.start();

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

this.setBounds(200,200,200,100);

this.setVisible(true);

}

public void actionPerformed(ActionEvent e){

Date date=new Date();

calendar.setTime(date);

if(e.getSource()==t){

label1.setText(date.toString());

if(calendar.get(Calendar.MINUTE)==0)//当分钟为0报时

if(calendar.get(Calendar.SECOND)=5){//持续5秒报时

if(calendar.get(Calendar.SECOND)%2==0)

label2.setText("现在是北京时间"+calendar.get(Calendar.HOUR_OF_DAY)+"点整。");

if(calendar.get(Calendar.SECOND)%2==1)

label2.setText("

");

}

}

}

public static void main (String[] args) {

new Test();

}

}

Java多线程代码,求注释,越详尽越好!有点急,谢谢!

这段代码的功能是显示各个时区当前时钟。

TimerListener是一个接口,有一个timeElapsed方法,目的是根据当前的时间绘制时钟,并刷新显示。

Timer继承Thread类,实现了run方法。run方法中,休眠指定的时间,并调用TimerListener的timeElapsed方法。如上例就是每休眠1S调用一次,所以看到的结果就是每1S绘制的时钟会更新一次。

ClockCanvas继承JPanel并实现了TimerListener接口,在构造方法中,根据指定的时区得到calendar实例。并开启线程Timer。

重写了paintComponent方法,在该方法中,首先绘制了一个圆,然后分别绘制时针、分针和秒针。

时针颜色为红色,分针为黄色,秒针为蓝色。在时钟下面绘制了城市,颜色为黑色。

java线程的经典代码

package threadgroup;

class ThreadDemo3 extends Thread {

private String name;

private int delay;

public ThreadDemo3(String sname, int i_delay) {

name = sname;

delay = i_delay;

}

public void run() {

try {

sleep(delay);

} catch (InterruptedException e) {

}

System.out.println("多线程测试!\n" + name + "\n" + delay);

}

}

public class testMyThread {

public static void main(String[] args) {

ThreadDemo3 th1,th2,th3;

th1 = new ThreadDemo3("线程1", (int) (Math.random() * 900));

th2 = new ThreadDemo3("线程2", (int) (Math.random() * 900));

th3 = new ThreadDemo3("线程3", (int) (Math.random() * 900));

th1.start();

th2.start();

th3.start();

}

}

package threadgroup;

public class threadDemo {

public static void main(String[] args) {

Thread t = Thread.currentThread();

t.setName("你好吗?");

System.out.println("正在进行的Thread是:" + t);

try {

for (int i = 0; i 5; i++) {

System.out.println("我不叫穆继超" + i);

Thread.sleep(3000);

}

} catch (Exception e) {

// TODO: handle exception

System.out.println("Thread has wrong" + e.getMessage());

}

}

}

package threadgroup;

public class threadDemo2 implements Runnable {

public threadDemo2() {

Thread t1 = Thread.currentThread();

t1.setName("第一个主进程");

System.out.println("正在运行" + t1);

Thread t2 = new Thread(this, "");

System.out.println("在创建一个进程");

t2.start();

try {

System.out.println("使他进入第一个睡眠状态");

Thread.sleep(2000);

} catch (InterruptedException e) {

System.out.println("Thread has wrong" + e.getMessage());

}

System.out.println("退出第一个进程");

}

public void run() {

try {

for (int i = 0; i 5; i++) {

System.out.println("进程" + i);

Thread.sleep(3000);

}

} catch (InterruptedException e) {

// TODO: handle exception

System.out.println("Thread has wrong" + e.getMessage());

}

System.out.println("退出第二个进程");

}

public static void main(String[] args) {

new threadDemo2();

}

}


新闻名称:java线程报时代码,线程的代码
当前路径:http://myzitong.com/article/dsioohs.html