樓上租慧的可以運(yùn)行

創(chuàng)新互聯(lián)專注于中站企業(yè)網(wǎng)站建設(shè),成都響應(yīng)式網(wǎng)站建設(shè),商城網(wǎng)站開(kāi)發(fā)。中站網(wǎng)站建設(shè)公司,為中站等地區(qū)提供建站服務(wù)。全流程按需網(wǎng)站建設(shè),專業(yè)設(shè)計(jì),全程項(xiàng)目跟蹤,創(chuàng)新互聯(lián)專業(yè)和態(tài)度為您提供的服務(wù)
也可以參考我的!
import java.lang.Thread;
public class ThreadDemo implements Runnable
{
Thread thread;
public ThreadDemo()
{
thread = new Thread(this);
thread.start();
}
public void run()
{
try
{
for( ; ; )
{
System.out.println(String.valueOf(Math.random()));
thread.sleep(1000);
}
}catch(Exception e){e.printStackTrace();}
}
public static void main(String[] args)
{
new ThreadDemo();
}
}
也衡型譽(yù)可以咐段運(yùn)行的!
呵呵
package?com.demo;
import?java.awt.BorderLayout;
import?java.awt.Dimension;
import?java.awt.event.ActionEvent;
import?java.awt.event.ActionListener;
import?java.util.Timer;
import?java.util.TimerTask;
import?javax.swing.JButton;
import?javax.swing.JFrame;
import?javax.swing.JTextField;
public?class?TimerTest?{
private?Timer?timer;
private?JTextField?滾春大field;
private?JButton?button;
private?森鍵boolean?flag?=?true;
public?TimerTest()?{
timer?=?new?Timer();
addview();
}
private?void?addview()?{
JFrame?frame?=?new?JFrame("Timer?test");
field?=?new?JTextField();
field.setPreferredSize(new?Dimension(0,?30));
button?=?new?JButton("start");
button.setPreferredSize(new?Dimension(100,?30));
button.addActionListener(new?ActionListener()?{
@Override
public?void?actionPerformed(ActionEvent?e)?{
if?(flag)?{
auto();
flag?=?false;
button.setText("stop");
}?else?{
timer.cancel();
flag?=?true;
button.setText("start");
}
}
});
frame.add(field,?BorderLayout.CENTER);
frame.add(button,?BorderLayout.EAST);
frame.setBounds(200,?200,?300,?60);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public?static?void?main(String[]?args)?{
new?大豎TimerTest();
}
private?void?auto()?{
timer.schedule(new?TimerTask()?{
@Override
public?void?run()?{
int?num?=?Integer.parseInt(field.getText().trim());
num?+=?1;
field.setText(num?+?"");
auto();
}
},?1000);
}
}
可以用 java.util.Timer(計(jì)時(shí)器) 以及 java.util.TimerTask(計(jì)時(shí)任務(wù)) 來(lái)實(shí)現(xiàn),具體代碼如下:
import?java.io.IOException;
import?java.util.Random;
import?java.util.Timer;
import?java.util.TimerTask;
public?class?Main?{
public?static?void?main(String[]?args)?throws?IOException,?InterruptedException?{
//?創(chuàng)建一個(gè)計(jì)時(shí)器
Timer?timer?=?new?Timer();
//?開(kāi)啟一個(gè)計(jì)時(shí)調(diào)度,延遲?0毫秒(也就是立即開(kāi)始執(zhí)行),調(diào)度評(píng)率:?1秒
timer.schedule(new?TimerTask()?{
@Override
虧源悄??????public?void?run()?{
//?生成隨機(jī)數(shù)邏輯
Random?r?=?new?Random();
裂芹????int?num?=?r.nextInt(10)?+?1;
System.out.println("隨機(jī)數(shù)為:"?+?num);
銷(xiāo)渣??????}
},?0L,?1000L);
//?timer.cancel();??//?關(guān)閉計(jì)時(shí)器
}
}
新聞標(biāo)題:一秒的代碼java 每秒代碼
本文鏈接:http://chinadenli.net/article18/dspjedp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站建設(shè)、靜態(tài)網(wǎng)站、定制網(wǎng)站、ChatGPT、云服務(wù)器、微信小程序
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)