當前位置:首頁 » 編程語言 » java定時線程

java定時線程

發布時間: 2022-09-26 09:31:24

java定時讓線程停止

我這兒有一個過年前寫的定時關機程序你可以拿去參考下~
程序里的取消功能跟你要的差不多

import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

/**
* 此類為定時關機的主界面類
* 這幾天一直都在搞我的聊天程序,哎 但是技術問題,一直都做不走
* 之前一直都想寫個定時關機程序,一直都是沒時間,
* 現在那邊做不走了,我就做做這個簡單的,找點自信
* @author 程勝
* @date 09年1月24
* @address 家
* @version 0.1
*
*/
public class ShutDown extends JFrame{
private static final long serialVersionUID = 1L;
private JPanel pCenter=new JPanel();
private JPanel pNorth=new JPanel();
private JPanel pSouth=new JPanel();
private JButton bExit=new JButton("退出");
//用於顯示當前時間
JLabel currentTime=new JLabel();
JButton bStart=new JButton("啟動");
JButton bCancel=new JButton("取消");
JLabel info=new JLabel("請設定關機時間…………");
//此下拉框用於選擇關機的小時
JComboBox hour=null;
//此下拉框用於先擇關機的分鍾
JComboBox minute=null;
/**
* 構造函數初始化界面
*/
public ShutDown(){
super("定時關機小程序verson0.1");
this.setSize(250,150);
addContent();
addEvent();
//改變大小不可用
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLocationRelativeTo(this);
this.setVisible(true);

}
/**
* 為按鈕添加事件監聽
*/
private void addEvent() {
ActionListener buttonListen=new SetEvent();
bStart.addActionListener(buttonListen);
bCancel.addActionListener(buttonListen);
bExit.addActionListener(buttonListen);

}
/**
* 為界面添加內容
*/
private void addContent() {
//新建一個匿名線程,用於顯示當前時間
new Thread(new Runnable(){
public void run(){
SimpleDateFormat sdf=new SimpleDateFormat("MM'月'dd'日'HH:mm:ss");
//Thread.interrupted()為判斷線程是否中斷
while(!Thread.interrupted()){
//獲取當前時間
Calendar current=Calendar.getInstance();
ShutDown.this.currentTime.setText("當前時間為:"+sdf.format(current.getTime()));
try {
//休眠一秒
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}

}
}).start();
//新建用於顯示小時和分鍾的字元串數組
String[] lHour=new String[24];
String[] lMinute=new String[60];

for(int i=0;i<24;i++){
lHour[i]=i+"";
}
for(int i=0;i<60;i++){
lMinute[i]=i+"";
}
hour=new JComboBox(lHour);
minute=new JComboBox(lMinute);
JLabel xiaoshi=new JLabel("時");
JLabel fen=new JLabel("分");
bCancel.setEnabled(false);
pNorth.setLayout(new FlowLayout());
pNorth.add(new JLabel("請設定關機時間:"));
pNorth.add(hour);pNorth.add(xiaoshi);
pNorth.add(minute);pNorth.add(fen);
pSouth.add(bStart);pSouth.add(bCancel);pSouth.add(bExit);
pCenter.setLayout(new GridLayout(2,1));
pCenter.add(currentTime);
pCenter.add(info);
this.add(pCenter,"Center");
this.add(pNorth,"North");
this.add(pSouth,"South");

}
/**
* @param args
*/
public static void main(String[] args) {
//設置外觀
JFrame.(true);

new ShutDown();

}
/**
* 內置類,用於處理事件
* @author 程勝
*
*/
class SetEvent implements ActionListener{
//用於存放關機的操作類
private Shut shut=null;
private String hour="";
private String minute="";
//獲得界面類
private ShutDown shutDown=ShutDown.this;
public void actionPerformed(ActionEvent e) {
//如果事件為啟動按鈕觸發
if(e.getSource()==bStart){
//獲取下拉框中的數據
hour=shutDown.hour.getSelectedItem().toString();
minute=shutDown.minute.getSelectedItem().toString();

handleStart();
}else{
//如果事件為取消按鈕觸發
if(e.getSource()==bCancel){
//取消操作
handleCancel();

}else{
//如果事件為退出按鈕觸發,則退出系統
System.exit(0);
}
}

}

/**
* 取消按鈕的事件的處理方法
*/
private void handleCancel() {
//如果關機類還在活動
if(shut.isAlive()){
//停止關機類線程
shut.stop();
}
//修改界面顯示
shutDown.info.setText("您取消了之前設定的定時關機!");
shutDown.hour.setEnabled(true);
shutDown.minute.setEnabled(true);
shutDown.bStart.setEnabled(true);
shutDown.bCancel.setEnabled(false);

}

/**
* 啟動按鈕的事件的處理方法
*/
private void handleStart() {

shutDown.info.setText("您設置的關機時間是"+(hour.length()==1?"0"+hour:hour)+"時"+(minute.length()==1?"0"+minute:minute)+"分");
shutDown.hour.setEnabled(false);
shutDown.minute.setEnabled(false);
shutDown.bStart.setEnabled(false);
shutDown.bCancel.setEnabled(true);
//新建並啟動關機線程
shut=new Shut(Byte.parseByte(hour),Byte.parseByte(minute));
shut.start();

}

}

}
/**
* 此類為實現關機操作的類,是一個線程類
* @author Administrator
*
*/
class Shut extends Thread{
//獲取當前日期及時間
private Calendar shutTime=Calendar.getInstance();
private Calendar currentTime;
private byte hour=0;
private byte minute=0;
public Shut(byte hour,byte minute){
this.hour=hour;
this.minute=minute;
}
/* 覆寫run方法
*
*/
public void run(){
//調用關機時間處理方法
handleTime();
while(!Thread.interrupted()){
//循環獲取系統時間
currentTime=Calendar.getInstance();
//如果關機時間在當前時間之前
if(shutTime.before(currentTime)){
//調用關機方法
shutDown();
//退出系統
System.exit(0);
}
try {
//線程休眠一秒
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
/**
* 關機方法
*/
private void shutDown() {
String shutdown="shutdown -s";
Runtime rt=Runtime.getRuntime();
try {
rt.exec(shutdown);
} catch (IOException e) {
e.printStackTrace();
}

}
/**
* 用於計算關機的時間
*/
private void handleTime() {
java.util.Date date=new java.util.Date();
if(date.getHours()>hour){
date.setDate(date.getDate()+1);
}if(date.getHours()==hour){
if(date.getMinutes()>minute){
date.setDate(date.getDate()+1);
}
}
date.setHours(hour);date.setMinutes(minute);
date.setSeconds(0);
shutTime.setTime(date);
}
}

Ⅱ java定時線程佔用伺服器資源嗎

會占資源,但是不會占很多。就是開一個線程的開銷。它sleep的時候,會釋放cpu的

Ⅲ java定時執行一個方法

現在能想到的是三種方法能實現:
1、普通thread實現 :是最常見的,創建一個thread,然後讓它在while循環里一直運行著,通過sleep方法來達到定時任務的效果。
2、TimerTask :啟動和去取消任務時可以控制,第一次執行任務時可以指定你想要的delay時間。
3、ScheledExecutorService實現 :最理想的定時任務實現方式,相比於Timer的單線程,它是通過線程池的方式來執行任務的,可以很靈活的去設定第一次執行任務delay時間,提供了良好的約定,以便設定執行的時間間隔等。
希望能幫到你。

Ⅳ java定時任務使用多線程webservcie執行了兩次這是為什麼

java Timer定時器是線程方式實現的。你可以把間隔時間調大點。如果操作資料庫的話,可以再資料庫的記錄上加上標記欄位,表示記錄正在處理。

Ⅳ java定時器與線程的區別

javax.swing.Timer
盡管所有 Timer 都使用一個共享線程(由第一個執行操作的 Timer 對象創建)執行等待,但是 Timer 的動作事件處理程序還會在其他線程(事件指派線程上)執行。這意味著 Timer 的操作處理程序可以安全地在 Swing 組件上執行操作。但是,它也意味著處理程序必須快速執行以保證 GUI 作出響應。
java.util.Timer
一種工具,線程用其安排以後在後台線程中執行的任務。可安排任務執行一次,或者定期重復執行。
簡單理解,javax.swing.Timer沒有增加線程,利用事件線程的間隙運行,java.util.Timer增加線程了。

Ⅵ java中怎麼讓一個線程在每天的23:30這個時刻執行一次(該程序啟動後就不會輕易停下來)

timer.schele(你的線程名稱,要執行的時間,24*60*60*1000); 你最好去看看timer定時器,以便了解更多

Ⅶ java中模擬一個定時炸彈線程,接收控制台輸入錯誤。

第一次運行時如果什麼都不輸入,直接Enter鍵,line會為空字元串。調用可能會出錯。

if(line.equals("quit")){
改為
if(line!=null &&line.equals("quit")){

Ⅷ 用java編程實現兩個定時線程,一個線程每隔1秒顯示一次,另一個線程每隔3秒顯示一次

public class Test {

Thread th1 = new Thread(new Th1());//定義線程1
Thread th2 = new Thread(new Th2());//定義線程2

public Test() {
this.th1.start();
this.th2.start();
}
public static void main(String[] args) {
new Test();
}
// 線程1
class Th1 implements Runnable {

public void run(){

while(true){
System.out.println("我是線程1");
try {
th1.sleep(1000);//一秒後再此運行
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
}
// 線程2
class Th2 implements Runnable {

public void run(){
while(true){
System.out.println("我是線程2");
try {
th2.sleep(3000);//3秒後再此運行
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
}
}

Ⅸ 如何使用Java線程執行定時任務及線程調動和同步

class Mytask1 extends Timertask { ①

public void run() {

System.out.println(」5 秒之後執行的定時器「);

}

}

Class Mytask2 extends Timertask {

public void run(){

System .out,println(」每秒執行的定時器」);

}

}

class Mytask3 extends Timertask {

public void run () {

System.out.println(」從某日起每分鍾執行的定時器!」) ;

}

}

public class TimerDemo {

plublic static void main(String[] args) {

Timer timer = new Timer( ) ; ②

timer.schele(new Mytask1( ) ,5000); ③

timer,schele(new Mytask1( ) ,1000,1000) ; ④

timer,schele(new Mytask1( ) ,new Dateo ( ) , 1000 * 60) ; ⑤

}

}

熱點內容
app什麼情況下找不到伺服器 發布:2025-05-12 15:46:25 瀏覽:714
php跳過if 發布:2025-05-12 15:34:29 瀏覽:467
不定時演算法 發布:2025-05-12 15:30:16 瀏覽:131
c語言延時1ms程序 發布:2025-05-12 15:01:30 瀏覽:166
動物園靈長類動物配置什麼植物 發布:2025-05-12 14:49:59 瀏覽:737
wifi密碼設置什麼好 發布:2025-05-12 14:49:17 瀏覽:150
三位數乘兩位數速演算法 發布:2025-05-12 13:05:48 瀏覽:399
暴風影音緩存在哪裡 發布:2025-05-12 12:42:03 瀏覽:545
access資料庫exe 發布:2025-05-12 12:39:04 瀏覽:632
五開的配置是什麼 發布:2025-05-12 12:36:37 瀏覽:367