这里重新开一个新帖,解决没解决的问题。通过点击,产生一个任务。如果上一个任务没有完成,则结束上一个任务,并创建一个新的任务。任务例子不是循环型的任务,仅仅是打印一个很长的序列.而且我不知道什么时候任务结束,只是点击按钮的时候,如果有正在执行中的任务,则结束掉,并创建一个新任务;如果没有正在执行的任务,则同样创建一个新的任务。现在的思路是利用Timer或者ScheduledExecutorService,但是我都没办法写出来。大家可以参考这个网址:http://topic.csdn.net/u/20120926/09/24ff4c3d-ec7b-42e9-97a6-febfae4afb3e.html希望大家帮助我程序中最后一个问题,先谢谢大家,祝大家中秋,国庆愉快。

解决方案 »

  1.   

    参考网址在这里:http://topic.csdn.net/u/20120926/09/24ff4c3d-ec7b-42e9-97a6-febfae4afb3e.html
      

  2.   


    package org;public class ArrayDemo {
    public static boolean isStart = false;
    public static void main(String[] args) {
    System.out.println("service is started...");
    Thread t = new BackThread();
    t.start();
    try {
    Thread.sleep(10000L);
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    if(isStart){
    t.stop();
    System.out.println("-------------华丽的分割线-------------");
    t = new BackThread();
    t.start();
    }
    try {
    Thread.sleep(5000L);
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    t.stop();
    System.out.println("service is end...");
    }
    }class BackThread extends Thread{
    public void run(){
    while(true){
    ArrayDemo.isStart = true;
    try {
    Thread.sleep(1000L);
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    System.out.println("service is running...");
    }
    }
    }
      

  3.   

    一样的呀,我这个只是说模拟你没完成的任务,while(true)能保证任务没有完成的时候,如果任务完成你把状态改为false就可以了,这样没什么太大难度吧
      

  4.   

    public void actionPerformed(ActionEvent e){
        scheduledExecutorService.shutdownNow();
        scheduledExecutorService = Executor.newXXX();
        scheduledExecutorService.schedule(..................);
    }
      

  5.   

    以下这个类中使用了可能不安全的Thread.stop();但可以达到你想要的效果在jdk 1.6.0_22下测试通过
    package timer;/**
     * 一个单任务调度器 
     */
    public class SingleTask { public MyThread thread = null;
    private static int threadId = 0;

    /**
     * 设置一个新任务,并清除以前已存在的任务
     * @param task
     * @param delay
     * @param period
     */
    public void setTask( Runnable task,long delay, long period){
    if(thread != null){
    try{
    thread.stop(); //多线程复杂环境下,要谨慎使用Thread.stop()
    }catch(Exception e){
    e.printStackTrace();
    }

    thread = null;
    }

    thread = new MyThread(task);
    thread.setName("MyThread-" + threadId ++ );
    thread.start();
    }


    class MyThread extends Thread{

    private Object lock = new Object();
    private long delay = 1000;
    private long period = 0;
    private Runnable task = null;

    public MyThread(Runnable runnable){
    this.task = runnable;
    }

    @Override
    public void run(){
    while(true){

    if(task != null){
    task.run();
    }

    //模拟Timer的周期执行
    if(period <= 0){
    break;
    }
    synchronized (lock) {
    try {
    lock.wait(period);
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    }
    }
    }

    //模拟Timer的延时启动
    public void start(){
    synchronized (lock) {
    try {
    lock.wait(delay);
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    }

    this.start();
    }
    }
    }
    package timer;/**
     * 模拟使用者
     */
    public class Test {
    private static Runnable task1 = new Runnable() {
    //任务一,无限制输出
    @Override
    public void run() {
    while(true){
    synchronized (task1) {
    System.out.println("output from task1..");
    }
    }

    }
    };

    private static Runnable task2 = new Runnable() {
    //任务二,每秒输出一次打印
    @Override
    public void run() {
    while(true){
    synchronized (task2) {
    System.out.println("===============output from task2..");
    try {
    task2.wait(1000);
    } catch (InterruptedException e) {
    e.printStackTrace();
    //break;
    }
    }
    }
    }
    };

    private static Object lock = new Object(); 
    public static void main(String args[]){
    SingleTask st = new SingleTask();
    st.setTask(task1, 1000, 0); //2秒后把任务1换成任务2
    synchronized (lock) {
    try {
    lock.wait(2000);
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    }
    st.setTask(task2, 1000, 0);
    }
    }
      

  6.   

    Thread.stop(); 在Java规范中已经被废止了,且存在不稳定可能性,比如:资源未能实际释放等问题。