Tread线程内每隔20分钟,执行一次方法
时间: 2024-04-29 14:23:14 浏览: 148
可以使用Java中的Timer和TimerTask类来实现每隔20分钟执行一次方法的功能。
示例代码如下:
```
import java.util.Timer;
import java.util.TimerTask;
public class MyThread extends Thread {
public void run() {
Timer timer = new Timer();
timer.schedule(new MyTask(), 0, 20 * 60 * 1000); // 每隔20分钟执行一次
}
class MyTask extends TimerTask {
public void run() {
// 执行需要执行的方法
}
}
}
```
在MyThread的run方法中创建一个Timer对象,并在其中调用schedule方法来定时执行MyTask任务。MyTask继承自TimerTask类,重写了run方法,在其中实现需要执行的方法即可。
相关问题
java编写一个应用程序创建两个线程,一个线程打印输出1~1000之间所有的奇数,另外一个线程打印输出1~1000之间所有的偶数,要求两个线程随机休眠一段时间后(1秒内)继续打印输出下一个数。分别使用Runnable接口和Tread类编程实现
使用Runnable接口实现代码如下:
```
public class PrintNumber implements Runnable {
private int start;
private int end;
public PrintNumber(int start, int end) {
this.start = start;
this.end = end;
}
public void run() {
for (int i = start; i <= end; i++) {
if (Thread.currentThread().getName().equals("Odd") && i % 2 == 1) {
System.out.println(Thread.currentThread().getName() + ": " + i);
} else if (Thread.currentThread().getName().equals("Even") && i % 2 == 0) {
System.out.println(Thread.currentThread().getName() + ": " + i);
}
try {
Thread.sleep((int) (Math.random() * 1000));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public class Main {
public static void main(String[] args) {
PrintNumber oddPrinter = new PrintNumber(1, 1000);
PrintNumber evenPrinter = new PrintNumber(1, 1000);
Thread oddThread = new Thread(oddPrinter, "Odd");
Thread evenThread = new Thread(evenPrinter, "Even");
oddThread.start();
evenThread.start();
}
}
```
使用Thread类实现代码如下:
```
public class PrintNumber extends Thread {
private int start;
private int end;
public PrintNumber(String name, int start, int end) {
super(name);
this.start = start;
this.end = end;
}
public void run() {
for (int i = start; i <= end; i++) {
if (Thread.currentThread().getName().equals("Odd") && i % 2 == 1) {
System.out.println(Thread.currentThread().getName() + ": " + i);
} else if (Thread.currentThread().getName().equals("Even") && i % 2 == 0) {
System.out.println(Thread.currentThread().getName() + ": " + i);
}
try {
Thread.sleep((int) (Math.random() * 1000));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public class Main {
public static void main(String[] args) {
PrintNumber oddThread = new PrintNumber("Odd", 1, 1000);
PrintNumber evenThread = new PrintNumber("Even", 1, 1000);
oddThread.start();
evenThread.start();
}
}
```
编写一个应用程序创建两个线程,一个线程打印输出1~1000之间所有的奇数,另外一个线程打印输出1~1000之间所有的偶数,要求两个线程随机休眠一段时间后(1秒内)继续打印输出下一个数。分别使用Runnable接口和Tread类编程实现。
使用Runnable接口实现:
```
public class PrintNumber implements Runnable {
private int start;
private int end;
private int increment;
public PrintNumber(int start, int end, int increment) {
this.start = start;
this.end = end;
this.increment = increment;
}
@Override
public void run() {
for (int i = start; i <= end; i += increment) {
System.out.println(Thread.currentThread().getName() + ": " + i);
try {
Thread.sleep((long) (Math.random() * 1000));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public class Main {
public static void main(String[] args) {
Thread t1 = new Thread(new PrintNumber(1, 1000, 2), "Even");
Thread t2 = new Thread(new PrintNumber(2, 1000, 2), "Odd");
t1.start();
t2.start();
}
}
```
使用Thread类实现:
```
public class PrintNumber extends Thread {
private int start;
private int end;
private int increment;
public PrintNumber(String name, int start, int end, int increment) {
super(name);
this.start = start;
this.end = end;
this.increment = increment;
}
@Override
public void run() {
for (int i = start; i <= end; i += increment) {
System.out.println(Thread.currentThread().getName() + ": " + i);
try {
Thread.sleep((long) (Math.random() * 1000));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public class Main {
public static void main(String[] args) {
Thread t1 = new PrintNumber("Even", 1, 1000, 2);
Thread t2 = new PrintNumber("Odd", 2, 1000, 2);
t1.start();
t2.start();
}
}
```
阅读全文