Skip to content

Commit db40be1

Browse files
committed
Version 1.4.1 - Add an AffinityThreadFactory to support ExecutorService
1 parent aacf187 commit db40be1

4 files changed

Lines changed: 110 additions & 1 deletion

File tree

README

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
[ Version History ]
22

3+
Version 1.4.1 - Add an AffinityThreadFactory to support ExecutorService
4+
35
Version 1.4 - Support binding of a whole core for hyper-threaded systems. AffinityLock.acquireCore()
46

57
Version 1.3 - Support thread layout strategies for using the same/different socket or cores.

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
<groupId>vanilla.java</groupId>
2424
<artifactId>affinity</artifactId>
25-
<version>1.4</version>
25+
<version>1.4.1</version>
2626
<packaging>jar</packaging>
2727

2828
<licenses>
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* Copyright 2011 Peter Lawrey
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package vanilla.java.affinity;
18+
19+
import java.util.concurrent.ThreadFactory;
20+
21+
/**
22+
* @author peter.lawrey
23+
*/
24+
public class AffinityThreadFactory implements ThreadFactory {
25+
private final String name;
26+
private final boolean daemon;
27+
private final AffinityStrategy[] strategies;
28+
private AffinityLock lastAffinityLock = null;
29+
private int id = 1;
30+
31+
public AffinityThreadFactory(String name, AffinityStrategy... strategies) {
32+
this(name, true, strategies);
33+
}
34+
35+
public AffinityThreadFactory(String name, boolean daemon, AffinityStrategy... strategies) {
36+
this.name = name;
37+
this.daemon = daemon;
38+
this.strategies = strategies;
39+
}
40+
41+
@Override
42+
public synchronized Thread newThread(final Runnable r) {
43+
String name2 = id <= 1 ? name : (name + '-' + id);
44+
id++;
45+
Thread t = new Thread(new Runnable() {
46+
@Override
47+
public void run() {
48+
AffinityLock al = lastAffinityLock == null ? AffinityLock.acquireLock() : lastAffinityLock.acquireLock(strategies);
49+
try {
50+
lastAffinityLock = al;
51+
r.run();
52+
} finally {
53+
al.release();
54+
}
55+
}
56+
}, name2);
57+
t.setDaemon(daemon);
58+
return t;
59+
}
60+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Copyright 2011 Peter Lawrey
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package vanilla.java.affinity;
18+
19+
import java.util.concurrent.Callable;
20+
import java.util.concurrent.ExecutorService;
21+
import java.util.concurrent.Executors;
22+
import java.util.concurrent.TimeUnit;
23+
24+
import static vanilla.java.affinity.AffinityStrategies.*;
25+
26+
/**
27+
* @author peter.lawrey
28+
*/
29+
public class AffinityThreadFactoryMain {
30+
private static final ExecutorService ES = Executors.newFixedThreadPool(4,
31+
new AffinityThreadFactory("bg", SAME_CORE, DIFFERENT_SOCKET, ANY));
32+
33+
public static void main(String... args) throws InterruptedException {
34+
for (int i = 0; i < 12; i++)
35+
ES.submit(new Callable<Void>() {
36+
@Override
37+
public Void call() throws InterruptedException {
38+
Thread.sleep(100);
39+
return null;
40+
}
41+
});
42+
Thread.sleep(200);
43+
System.out.println("\nThe assignment of CPUs is\n" + AffinityLock.dumpLocks());
44+
ES.shutdown();
45+
ES.awaitTermination(1, TimeUnit.SECONDS);
46+
}
47+
}

0 commit comments

Comments
 (0)