導航:首頁 > 編程語言 > javatimer變化

javatimer變化

發布時間:2021-02-28 05:28:12

Ⅰ 怎麼用java中的timer做出延遲效果

new Thread(new Runnable() {
public void run() {
while(true) {
repaint();
Thread.sleep(500);
}
}
}).start();
這樣就沒錯誤了。

要用延遲的話,不妨試試java.util.Timer().
new java.util.Timer().schele(new TimerTask(){
public void run() {
//這里寫延遲後要運行的代碼
repaint();
//如果專只要這個延遲一次,用cancel方法屬取消掉.
this.cancel();

}}, 3000);
//參考參考java幫助文擋,也可以以固定間隔連續執行.
復制別人的過來的,望採納

Ⅱ java用Timer計時,是否我每次執行下面語句,倒計時就重新計算一次

是的,因為你每次都new了一個新的timer

Ⅲ java timer 用法

import java.util.Timer;

public class TimerTest1 {

static class MyTask extends java.util.TimerTask {
void run() {
System.out.println("開始");
}

}

static class Shutdown extends java.util.TimerTask {
Timer timer = null;

public Shutdown() {
}

public Shutdown(Timer mytimer) {

timer = mytimer;
}

public void run() {
System.out.println("結束");
timer.cancel();// 使用這個方法退出任務
}

}

public static void main(String[] args) {
Timer timer = new Timer();
timer.schele(new MyTask(), 1000);// 在1秒後執行此任務
timer.schele(new Shutdown(timer), 3000);// 在3秒後執行此任務
}

}

Ⅳ java timer類實現隨機時間調用方法

//一秒後開始執行,每五秒內隨機執行一次.
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;
import java.util.Date;
public class test1{
public static void main(String[] args) throws Exception{
Timer time=new Timer();
time.schele(new tast(),1000,5000);
}
}

class tast extends TimerTask{
public void run(){
Date date=new Date();
System.out.println("I'm start!"+date.toString());
Random random=new Random();
try{
Thread.sleep(random.nextInt(5000));
}catch(Exception e){
e.printStackTrace();
}
date=new Date();
System.out.println("I'm execute!"+date.toString());

}
}

Ⅳ java 怎麼讓一個變數隨時間變化

java定時器

importjava.util.Random;
importjava.util.Timer;
importjava.util.TimerTask;

/**
*@authorAdministrator
*@Description:TODO
*@date2016年5月1日下午2:48:31
*/
publicclassMainEntry{
;

publicvoidsetVal(){
Timertimer=newTimer();
//每隔一秒生成一個[1,100)內的隨機整數,賦給成員a
timer.schele(newTimerTask(){
@Override
publicvoidrun(){
Randomrand=newRandom();
setA(rand.nextInt(100));
System.out.println("a:"+getA());
}
},1000,1000);
}

publicvoidsetA(inta){
this.a=a;
}

publicintgetA(){
returna;
}

publicstaticvoidmain(String[]args){
MainEntryme=newMainEntry();
me.setVal();
}
}

Ⅵ java timer定時執行速度越來越快

不知道你要實現怎樣的功能,你看這樣可行?
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.Timer;
import javax.swing.JPanel;

public class as extends JFrame {

public static void main(String[] args) {
// TODO Auto-generated method stub
as frame = new as();
jump jump = new jump();
frame.add(jump);
frame.setTitle("跳刺");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(800, 250);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
frame.setResizable(false);
jump. setBackground(Color.white);

}

}
class jump extends JPanel {
int a = 794 ;
int delay =1000;
ActionListener listener = new ActionListener(){
public void actionPerformed(ActionEvent arg0){

repaint();

}
};
Timer timer = new Timer(delay,listener);
public void refresh(){

if(!timer.isRunning()) timer.start();
}

protected void paintComponent(Graphics g) {
super.paintComponent(g);
System.out.print(a+" ");
refresh();
}

}

Ⅶ java applent 實現timer事件

代碼。
--------------------------------------------
import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

public class AppletDemo extends Applet {

Color[] colors = { Color.RED, Color.GREEN, Color.BLUE };
int index = 0;

public void init() {
.init();
setSize(200, 200);
Timer timer = new Timer();
timer.schele(new ColorChange(), new Date(), 500);
}

public void paint(Graphics g) {
super.paint(g);
if (index == colors.length) {
index = 0;
}
Color color = colors[index++];
g.setColor(color);
g.drawString("hello", 10, 10);
g.drawString("Color", 10, 30);
g.drawString("wait", 10, 50);
}

class ColorChange extends TimerTask {
public void run() {
repaint();
}
}
}

Ⅷ java timer運行情況

weblogic裡面就像是進程一樣.

你點一下後台,會發現這個窗口標題欄出現,標記或者專,選定字樣

這時你可以復制屬其中的內容.

當你點擊回車或者單擊滑鼠右鍵時,就會消失.

timer運行之所以終止是因為你選定了後台,這樣weblogic里的進程就會終止運行,把後台鎖定,該服務將處於停止狀態,只有解鎖之後,才會繼續運行.

Ⅸ java定時任務的使用timer怎麼讓間隔隨時變,也就是按著用戶從頁面輸入的時間,不定期每周或每

使用一個類整數制變數,隨輸入確定這個定時。
int seconds=0;
public void run(){
seconds++;
if(seconds==120){
//到定時的時間
}
}

~~~~~~~~~~~~~~~~~~

閱讀全文

與javatimer變化相關的資料

熱點內容
蘋果ipad能連鍵盤嗎 瀏覽:492
jsreference 瀏覽:637
蘋果用什麼數據線 瀏覽:748
越語電影 瀏覽:55
電影結束了?英文翻譯 瀏覽:476
Excel數據文件夾 瀏覽:442
java交行面試題 瀏覽:626
露點電影合集 瀏覽:697
cad文件可以用p3d打開嗎 瀏覽:394
文件名不能用的特殊符號 瀏覽:740
那能找到在線小電影 瀏覽:476
穿越朱元璋的兒子小說 瀏覽:35
the beast 1975 瀏覽:491
文件夾里可以有多少子文件夾 瀏覽:384
泰國女的販毒電影 瀏覽:955
javatext包 瀏覽:542
韓國倫理電影合夥殺害男友 瀏覽:438
豆比羊電影怎麼看不了 瀏覽:585
網站app在線看百度 瀏覽:843
脫軌微電影 瀏覽:966

友情鏈接