- 浏览: 126942 次
- 性别:
- 来自: Singapore
文章分类
- 全部博客 (112)
- Tiger Thread (18)
- Perforce (6)
- Spring (5)
- maven (3)
- log4j (3)
- Quartz Scheduler (4)
- unix and linux (12)
- hibernate (3)
- Enum (1)
- Futures and Options (1)
- Market Making (2)
- Java Basic (11)
- Tibco EMS (3)
- F I X message (5)
- equity derivative (2)
- Sybase (3)
- XML (1)
- JUnit (2)
- J A X B 2.0 (1)
- N I O (1)
- windows batch file (1)
- Cruise Control (1)
- util Class (5)
- ant (1)
- JMS (1)
- profiling (0)
- Sql Server (6)
- GXT (2)
- eclipse (1)
- Generics (1)
- Tibco RV (3)
- Autosys (0)
- Message (1)
最新评论
-
houzhe11:
<property name="proxyTa ...
AOP usage -- BeanNameAutoProxyCreator usage
Executor can return Executor, ExecutorService, and ScheduledExecutorService.
This is very, very usful Tiger new timer.
scheduler.scheduleAtFixedRate(new TimePrinter(System.out), 0, 10, SECONDS);
4 arguments:
1) Runable or Callable, which is the task scheduled to run.
2) 0. When the first time to run. 0 seconds later, means, immediately.
3) 10. When next time run. 10 seconds later.
4) What's time unit. The argument 0, and 10, all use the time unit SECONDs.
package test;
import java.io.PrintStream;
import java.util.Date;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import static java.util.concurrent.TimeUnit.SECONDS;;
public class Thread5
{
public static void main(String[] args)
{
// Get the scheduler
//An alternative way to get scheduler is like this, you can simply replace with it
//ScheduledThreadPoolExecutor scheduler = new ScheduledThreadPoolExecutor(10);
ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();
// Get a handle, starting now, with a 10 second delay
final ScheduledFuture<?> timeHandle = scheduler.scheduleAtFixedRate(new TimePrinter(System.out), 0, 10, SECONDS);
// Schedule the event, and run for 1 hour (60 * 60 seconds)
scheduler.schedule(new Runnable()
{
public void run()
{
timeHandle.cancel(false);
}
}, 60 * 60, SECONDS);
}//end of main
}
class TimePrinter implements Runnable
{
private PrintStream out;
public TimePrinter(PrintStream out)
{
this.out = out;
System.out.println("a TimePrinter instance is created");
}
public void run()
{
out.printf("Current time: %tr%n", new Date());
}
}
Summary:
Step 1) Get a ScheduledExecutorService --- Scheduler
Step 2) Tell the scheduler, which to run, how frequent, when to start. ---generate ScheduledFuture
Step 3) Start the Scheduler using schedule() method, wraping ScheduledFuture into Runnable, passing the Runnable to the schedule()
发表评论
-
javadoc for Cyclic Barrier
2009-04-24 12:48 904java.util.concurrent.CyclicBarr ... -
Delayed interface and Delay Queue
2009-04-22 17:42 1053/** * A standard implementati ... -
Count Down Latch example code
2009-04-22 10:38 1155Key point : 1) 1 task is co ... -
3 ways to break dead lock
2009-04-21 17:30 7621) supply special resources. ... -
Blocking Queue Usage
2009-04-20 11:21 8333 implementations: LinkedBlocki ... -
The usage of Lock and Condition
2009-04-18 12:31 1069//: concurrency/waxomatic2/WaxO ... -
Re entrantLock usage
2009-04-15 17:15 1320a thread can be really interru ... -
new interrupt in java5
2009-04-15 12:08 658In Java 5, Thread.interrupt() i ... -
interrupt
2009-04-15 10:57 8171) Each thread has a boolean in ... -
Executor Service Usage
2009-04-14 18:18 894ExecutorService exec = Executor ... -
Thread Local usage
2009-04-14 17:46 817ThreadLocal usage – from Javado ... -
Timer TimerTask usage
2009-04-14 12:03 724Timer typical usage new Tim ... -
wait, notify及线程通讯机制
2009-02-26 22:42 8501) wait(), notify() 方法被调用的时候,只要 ... -
Java Thread programming basical knowledge
2009-02-26 22:40 1089yield() : Give a hint to the th ... -
Count Down Latch explanation
2008-10-02 10:29 957Very important paragraph on how ... -
Executor usage
2008-07-22 11:04 903Executor is used to arrange thr ... -
Callable Usage
2008-07-22 10:24 933The important thing need to loo ...
相关推荐
@Service public class PrintJob { @Scheduled(initialDelay=3000, fixedDelay = 10000) public void print() { // 打印任务逻辑 } } ``` 2.2. **Spring Boot 中的并行调度** 要实现并行调度,可以创建一...
在"Java Spring多线程demo代码"中,可能包含使用`@Async`和`@Scheduled`注解的示例,以及如何配置和自定义`ThreadPoolTaskExecutor`和`ThreadPoolTaskScheduler`的代码。通过这些示例,你可以学习到如何在Spring应用...
Spring 3.0引入了`@Scheduled`注解来支持定时任务,并通过`@Async`注解实现了异步方法的调用。下面将详细解释这两个功能以及如何在实际应用中使用它们。 ### 定时任务(定时) Spring的定时任务功能主要基于`org....
Spring提供了一个名为`@Scheduled`的注解,可以方便地将方法标记为定时任务。 1. **使用@Scheduled** - 首先,我们需要配置一个`TaskScheduler`或`ScheduledExecutorService`,并在Spring配置文件中声明。 - 然后...
这个注解会启动一个后台任务调度器,自动检测带有`@Scheduled`注解的方法并按需执行。 ```java @Configuration @EnableScheduling public class TaskConfig { // ... } ``` 接下来,我们来讨论线程配置。默认情况...
@Service public class AsyncService { @Async("taskExecutor") // 指定使用哪个TaskExecutor public Future<String> executeAsyncTask() throws InterruptedException { Thread.sleep(2000); // 模拟耗时操作 ...
@Service public class AsyncService { @Async public void asyncTask() { // 执行耗时操作 } } ``` 3. **ThreadPoolTaskScheduler**: 类似于`ThreadPoolTaskExecutor`,但`ThreadPoolTaskScheduler`主要...
这意味着如果存在多个定时任务(例如使用`@Scheduled`注解定义的任务),那么这些任务将会依次执行,即一个任务必须完全执行完毕后,下一个任务才能开始执行。这种执行方式可能会导致资源浪费,并且降低系统的并发...
@Service public class AsyncService { private final Queue<String> taskQueue = new ConcurrentLinkedQueue(); // 使用@Async标记为异步方法 @Async("asyncExecutor") public void processTask(String task)...
@Service public class UserService { @Autowired private UserRepository userRepository; public Pageable getUsers(Pageable pageable) { return userRepository.findAll(pageable); } } ``` 前端页面则...
在Spring配置文件中,你可以通过`<task:executor>`和`<task:scheduler>`元素定义`TaskExecutor`和`TaskScheduler`,并配置相关属性。在Spring Boot应用中,可以通过`@EnableScheduling`注解开启任务调度,然后使用`@...
<task:annotation-driven executor="executor" scheduler="scheduler"/> ``` - `<task:executor>`: 定义一个任务执行器,用于执行定时任务。 - `<task:scheduler>`: 定义一个调度器,用于管理定时任务的调度。 - ...
public Executor getAsyncExecutor() { ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor(); taskExecutor.setCorePoolSize(5); // 核心线程数 taskExecutor.setMaxPoolSize(20); // 最大线程...
@Scheduled(fixedRate = 60000) // 每60秒执行一次 public void executeTask() { // 你的任务代码 } } ``` 3. **在Tomcat的Context.xml中配置定时任务**: - 另一种方法是在Tomcat的Context配置文件中使用`...
public Executor taskExecutor() { return Executors.newFixedThreadPool(10); // 创建线程池 } } ``` 然后在需要异步执行的方法上添加`@Async`: ```java @Service public class AsyncService { @Async ...
- **@Async**标记一个方法为异步执行,Spring将使用AsyncConfigurer配置的Executor来执行该方法。 7. **定时任务相关** - **@Scheduled**注解在方法上,可以实现定时任务,配合@EnableScheduling开启定时任务。 ...