1. 程式人生 > >JAVA定時器和多執行緒

JAVA定時器和多執行緒

文章目錄

這篇部落格介紹java的定時器類Timer, 和多執行緒類Thread.

任務一:

  • 完成一個java application應用程式,使用定時器程式設計,在實時顯示當前時間,每1秒時鐘內容更新一次。

    主要方法:

Modifier Constructor Description
protected TimerTask() Creates a new timer task.
Timer() Creates a new timer.
Method Description
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 Type Method Description
static void sleep(long millis) Causes the currently executing thread to sleep (temporarily cease execution) for the specified number of milliseconds
void start() Causes this thread to begin execution; the Java Virtual Machinecalls 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();
				}
			}
		}
	}
}

執行結果:

在這裡插入圖片描述