java中Time和TimeTask的使用
答案:3 悬赏:0 手机版
解决时间 2021-03-22 08:46
- 提问者网友:我是女神我骄傲
- 2021-03-21 15:56
java中Time和TimeTask的使用
最佳答案
- 五星知识达人网友:深街酒徒
- 2021-03-21 16:16
代码如下:
public class MusicTitle {
String title ="Notitle.";
String artist="No artist.";
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getArtist() {
return artist;
}
public void setArtist(String artist) {
this.artist = artist;
}
}
public class MusicStore {
String owner="Noowner.";
int openTime =0;
int closeTime=0;
public String getOwner() {
return owner;
}
public void setOwner(String owner) {
this.owner = owner;
}
public void setOpen(int openTime){
this.openTime=openTime;
}
public int getOpen(){
return openTime;
}
public void setClose(int closeTime){
this.closeTime=closeTime;
}
public int getClose(){
return closeTime;
}
public boolean isOpen(){
AltDate AltDate=new AltDate();
int nowtime = AltDate.getHourInt() ;
if(nowtime>=this.getOpen() && nowtime<=this.getClose()){
return true;
}
else{
return false;
}
}
public String getOpenClosedMessage(){
if(isOpen()){
return "We're Open!";
}else{
return "We're Closed!";
}
}
public void displayHoursOfOperation(){
System.out.println("StoreHours:");
System.out.println("Daily:"+this.getOpen()+":00 AM-"+this.getClose()+":00 PM");
}
public String toString(){
return "Owneris:"+ getOwner();
}
MusicTitle[] title=null;
public MusicTitle[] getTitle() {
return title;
}
public void setTitle(MusicTitle[] title) {
this.title = title;
}
public void displayMusicTitles(){
for(int i=0;i System.out.println("Title"+(i+1)+":");
System.out.println("Title:"+title[i].getTitle());
System.out.println("Artist:"+title[i].getArtist());
}
}
}
public class AltDate {
@SuppressWarnings("deprecation")
public int getHourInt(){
java.util.Date date=new java.util.Date();
return date.getHours();
}
}
import java.util.Scanner;
public class TestMusicStore {
public static void main(String[] args) {
// TODO 自动生成的方法存根
MusicStore ms=new MusicStore() ;
Scanner in=new Scanner(System.in);
ms.setOwner(in.next());
ms.setOpen(in.nextInt());
ms.setClose(in.nextInt());
MusicTitle[] mt=new MusicTitle[2];
for(int i=0;i<2;i++){
mt[i].setTitle(in.next());
mt[i].setArtist(in.next());
}
ms.setTitle(mt);
ms.getOpenClosedMessage();
ms.displayHoursOfOperation();
System.out.println(ms.toString());
ms.displayMusicTitles();
System.exit(0);
}
}
public class MusicTitle {
String title ="Notitle.";
String artist="No artist.";
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getArtist() {
return artist;
}
public void setArtist(String artist) {
this.artist = artist;
}
}
public class MusicStore {
String owner="Noowner.";
int openTime =0;
int closeTime=0;
public String getOwner() {
return owner;
}
public void setOwner(String owner) {
this.owner = owner;
}
public void setOpen(int openTime){
this.openTime=openTime;
}
public int getOpen(){
return openTime;
}
public void setClose(int closeTime){
this.closeTime=closeTime;
}
public int getClose(){
return closeTime;
}
public boolean isOpen(){
AltDate AltDate=new AltDate();
int nowtime = AltDate.getHourInt() ;
if(nowtime>=this.getOpen() && nowtime<=this.getClose()){
return true;
}
else{
return false;
}
}
public String getOpenClosedMessage(){
if(isOpen()){
return "We're Open!";
}else{
return "We're Closed!";
}
}
public void displayHoursOfOperation(){
System.out.println("StoreHours:");
System.out.println("Daily:"+this.getOpen()+":00 AM-"+this.getClose()+":00 PM");
}
public String toString(){
return "Owneris:"+ getOwner();
}
MusicTitle[] title=null;
public MusicTitle[] getTitle() {
return title;
}
public void setTitle(MusicTitle[] title) {
this.title = title;
}
public void displayMusicTitles(){
for(int i=0;i
System.out.println("Title:"+title[i].getTitle());
System.out.println("Artist:"+title[i].getArtist());
}
}
}
public class AltDate {
@SuppressWarnings("deprecation")
public int getHourInt(){
java.util.Date date=new java.util.Date();
return date.getHours();
}
}
import java.util.Scanner;
public class TestMusicStore {
public static void main(String[] args) {
// TODO 自动生成的方法存根
MusicStore ms=new MusicStore() ;
Scanner in=new Scanner(System.in);
ms.setOwner(in.next());
ms.setOpen(in.nextInt());
ms.setClose(in.nextInt());
MusicTitle[] mt=new MusicTitle[2];
for(int i=0;i<2;i++){
mt[i].setTitle(in.next());
mt[i].setArtist(in.next());
}
ms.setTitle(mt);
ms.getOpenClosedMessage();
ms.displayHoursOfOperation();
System.out.println(ms.toString());
ms.displayMusicTitles();
System.exit(0);
}
}
全部回答
- 1楼网友:woshuo
- 2021-03-21 17:31
public static void main(String[] args) {
new java.util.Timer().schedule(new java.util.TimerTask(){
int i=0;
@Override
public void run() {
// TODO 自动生成的方法存根
//这里写要执行的任务逻辑
if(i<10)
{
//重复执行的任务
System.out.println(13+" i-"+i);
}
else
{
System.out.println("end");
System.out.println(2/0);//用抛出异常的方法结束定时器线程,不影响主线程;
//如果不想停止定时器线程,可调用如下方法:
//this.cancel();
}
i++;
}
},
1000L,//多久后任务触发(此处是1秒)
1000*60*30//间隔多久重复执行任务(此处是30分钟)
);
}
new java.util.Timer().schedule(new java.util.TimerTask(){
int i=0;
@Override
public void run() {
// TODO 自动生成的方法存根
//这里写要执行的任务逻辑
if(i<10)
{
//重复执行的任务
System.out.println(13+" i-"+i);
}
else
{
System.out.println("end");
System.out.println(2/0);//用抛出异常的方法结束定时器线程,不影响主线程;
//如果不想停止定时器线程,可调用如下方法:
//this.cancel();
}
i++;
}
},
1000L,//多久后任务触发(此处是1秒)
1000*60*30//间隔多久重复执行任务(此处是30分钟)
);
}
- 2楼网友:时间的尘埃
- 2021-03-21 17:18
static int i = 0;
Timer timer = new Timer();
timer.schedule(new MyTask(),1000,30*60*1000);//定义一个定时器,一秒后运行,每隔30分钟运行一次。
public class MyTask extend TimerTask(){
......................................//这是执行的代码
i = i+1;
if(i>=10){
timer.cancel();//关闭定时器
}
}
Timer timer = new Timer();
timer.schedule(new MyTask(),1000,30*60*1000);//定义一个定时器,一秒后运行,每隔30分钟运行一次。
public class MyTask extend TimerTask(){
......................................//这是执行的代码
i = i+1;
if(i>=10){
timer.cancel();//关闭定时器
}
}
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯