JAVA定时器和多线程


这篇博客介绍java的定时器类Timer, 和多线程类Thread.

任务一:

  • 完成一个java application应用程序,使用定时器编程,在实时显示当前时间,每1秒时钟内容更新一次。

    主要方法:

ModifierConstructorDescription
protectedTimerTask()Creates a new timer task.
Timer()Creates a new timer.
MethodDescription
scheduleAtFixedRate(TimerTask task, long delay, long period)Schedules the specified task for repeated fixed-rate execution,beginning after the specified delay.
run()The action to be performed by this timer task.

程序:

import java.util.Timer;
import java.util.TimerTask;
import java.util.Date;

public class JavaTimer {
	public static void main(String args[]) {
		TimerTask task = new TimerTask() { //创建一个新的timer task 
			public void run() { //定时器任务执行的操作
				Date date = new Date();//创建Date对象
				String hour = String.format("%tH",date);//格式化输出时间
				String minute = String.format("%tM",date);
				String second = String.format("%tS",date);
				
				System.out.println("现在是:" + hour + "时" +minute+"分" + second +"秒");
				
			}
		};
		
		Timer timer = new Timer();//创建一个定时器
		long delay = 0;
		long PeriodTime = 1 * 1000;
		timer.scheduleAtFixedRate(task, delay, PeriodTime);
		//重复执行特定任务,第一个参数为要执行的任务,第二个为执行任务之前延迟的时间,第三个为时间间隔
		//单位都是毫秒
	}
}

运行结果:

在这里插入图片描述

任务二:

  • 完成一个java application应用程序,在应用程序主进程中新开一个线程,此线程进行死循环,每1秒被激活一次,激活时即在输出显示当前时间。

主要方法:

Modifier and TypeMethodDescription
static voidsleep(long millis)Causes the currently executing thread to sleep (temporarily cease execution) for the specified number of milliseconds
voidstart()Causes this thread to begin execution; the Java Virtual Machine
calls the run method of this thread.

程序:

import java.text.SimpleDateFormat;
import java.util.Date;
public class ThreadTest extends Thread {
	
	public void run() {
		while(true) {
			try {
				Thread.sleep(1000);//程序眠1秒,括号内参数以毫秒为单位
				SimpleDateFormat ft = new SimpleDateFormat("yyy-MM-dd hh:mm:ss");//格式化输出时间
				String time = ft.format(new Date());
				System.out.println("现在时刻:" + time);//输出时间
				
			}
			catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
	public static void main(String[] args) {
		new ThreadTest().start();//启动线程
	}
}

运行结果:

在这里插入图片描述

任务三:

  • 完成一个java application应用程序,此应用程序公共类有一个double型类属性(变量)x,初始值为0;在应用程序主进程中新开两个线程,这两个线程都进行死循环;第1个线程每隔300ms激活一次,令类属性x自加1.0并输出显示;第2个线程每隔400ms激活一次,令类属性x自加0.1并输出显示。

主要方法:

分别开启两个线程

程序:

public class ThreDemo {
	static double x = 0;
	public static void main(String args[]) {
		Thread t1 = new Thread(new Thread1());//创建线程
		Thread t2 = new Thread(new Thread2());
		
		t1.start();//开启线程
		t2.start();
		
	}
	public static class Thread1 extends Thread {
		public void run() { //run方法,里面包含要执行的任务
			while(true){
				try{
					Thread.sleep(300);//线程休眠300毫秒
					x +=1.0;
					System.out.println("x+1: " + x);//输出值
				}
				catch(InterruptedException e) {
					e.printStackTrace();
				}
			}
		}
	}
	public static class Thread2 extends Thread {
		public void run() {
			while(true){
				try{
					Thread.sleep(300);
					x +=0.1;
					System.out.println("x+0.1: " + x);
				}
				catch(InterruptedException e) {
					e.printStackTrace();
				}
			}
		}
	}
}

运行结果:

在这里插入图片描述

  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值