⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 scheduledexecutorservicetest.java

📁 用NETBEANS做的一个关于Java的小小的demo.大家赐教
💻 JAVA
字号:
/*
 * ScheduledExecutorServiceTest.java
 *
 * Created on 2007年9月14日, 上午1:09
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package newThread;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;

/**
 *
 * @author Administrator
 */
public class ScheduledExecutorServiceTest {
    
    public static void main(String[] args)
    
    throws InterruptedException, ExecutionException{
        
        //*1
        
        ScheduledExecutorService service = Executors.newScheduledThreadPool(2);
        
        //*2
        
        Runnable task1 = new Runnable() {
            
            public void run() {
                
                System.out.println("Task repeating.");
                
            }
            
        };
        
        //*3
        
        final ScheduledFuture future1 =
                
                service.scheduleAtFixedRate(task1, 0, 1, TimeUnit.SECONDS);
        
        //*4
        
        ScheduledFuture<String> future2 = service.schedule(new Callable<String>(){
            
            public String call(){
                
                future1.cancel(true);
                
                return "task cancelled!";
                
            }
            
        }, 5, TimeUnit.SECONDS);
        
        System.out.println(future2.get());
        
//*5
        
        service.shutdown();
        
    }
    
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -