Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 4e1324f

Browse files
author
Rajeev Kumar Singh
committed
Java Executors and Thread Pool
1 parent 62e97c2 commit 4e1324f

File tree

3 files changed

+66
-0
lines changed

3 files changed

+66
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import java.util.concurrent.ExecutorService;
2+
import java.util.concurrent.Executors;
3+
4+
/**
5+
* Created by rajeevkumarsingh on 09/05/17.
6+
*/
7+
public class ExecutorsExample {
8+
public static void main(String[] args) {
9+
System.out.println("Inside : " + Thread.currentThread().getName());
10+
11+
// Create an executor service with a thread pool of size one
12+
System.out.println("Creating Executor Service...");
13+
ExecutorService executorService = Executors.newSingleThreadExecutor();
14+
15+
System.out.println("Creating a Runnable...");
16+
Runnable runnable = () -> {
17+
System.out.println("Inside : " + Thread.currentThread().getName());
18+
};
19+
20+
System.out.println("Submit the task specified by the runnable to the executor service.");
21+
executorService.submit(runnable);
22+
23+
System.out.println("Shutting down the executor");
24+
executorService.shutdown();
25+
}
26+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import java.util.concurrent.Executors;
2+
import java.util.concurrent.ScheduledExecutorService;
3+
import java.util.concurrent.TimeUnit;
4+
5+
/**
6+
* Created by rajeevkumarsingh on 10/05/17.
7+
*/
8+
public class ScheduledExecutorsExample {
9+
public static void main(String[] args) {
10+
ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(1);
11+
Runnable task = () -> {
12+
System.out.println("Executing Task At " + System.nanoTime());
13+
};
14+
15+
System.out.println("Submitting task at " + System.nanoTime() + " to be executed after 5 seconds.");
16+
scheduledExecutorService.schedule(task, 5, TimeUnit.SECONDS);
17+
18+
scheduledExecutorService.shutdown();
19+
}
20+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import java.util.concurrent.Executors;
2+
import java.util.concurrent.ScheduledExecutorService;
3+
import java.util.concurrent.TimeUnit;
4+
5+
/**
6+
* Created by rajeevkumarsingh on 10/05/17.
7+
*/
8+
public class ScheduledExecutorsPeriodicExample {
9+
public static void main(String[] args) {
10+
ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(1);
11+
12+
Runnable task = () -> {
13+
System.out.println("Executing Task At " + System.nanoTime());
14+
};
15+
16+
System.out.println("scheduling task to be executed every 2 seconds with an initial delay of 0 seconds");
17+
scheduledExecutorService.scheduleAtFixedRate(task, 0,2, TimeUnit.SECONDS);
18+
19+
}
20+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /