| 1 | package org.dcache.util; |
| 2 | |
| 3 | import java.util.concurrent.ThreadFactory; |
| 4 | |
| 5 | import dmg.cells.nucleus.CDC; |
| 6 | |
| 7 | /** |
| 8 | * CDCThreadFactory decorates another ThreadFactory and makes all |
| 9 | * threads CDC aware. Each thread inherits the CDC of the thread |
| 10 | * calling the newThread method. |
| 11 | */ |
| 12 | public class CDCThreadFactory implements ThreadFactory |
| 13 | { |
| 14 | private ThreadFactory _factory; |
| 15 | |
| 16 | public CDCThreadFactory(ThreadFactory factory) |
| 17 | { |
| 18 | _factory = factory; |
| 19 | } |
| 20 | |
| 21 | public Thread newThread(final Runnable r) |
| 22 | { |
| 23 | final CDC cdc = new CDC(); |
| 24 | return _factory.newThread(new Runnable() { |
| 25 | public void run() |
| 26 | { |
| 27 | cdc.apply(); |
| 28 | try { |
| 29 | r.run(); |
| 30 | } finally { |
| 31 | CDC.clear(); |
| 32 | } |
| 33 | } |
| 34 | }); |
| 35 | } |
| 36 | } |