1 16 17 package org.springframework.util; 18 19 30 public class CustomizableThreadCreator { 31 32 private final Object monitor = new Object (); 33 34 private String threadNamePrefix; 35 36 private int threadPriority = Thread.NORM_PRIORITY; 37 38 private boolean daemon = false; 39 40 private ThreadGroup threadGroup; 41 42 private int threadCount = 0; 43 44 45 48 public CustomizableThreadCreator() { 49 this(null); 50 } 51 52 56 public CustomizableThreadCreator(String threadNamePrefix) { 57 setThreadNamePrefix(threadNamePrefix); 58 } 59 60 61 65 public void setThreadNamePrefix(String threadNamePrefix) { 66 this.threadNamePrefix = (threadNamePrefix != null ? threadNamePrefix : getDefaultThreadNamePrefix()); 67 } 68 69 73 public String getThreadNamePrefix() { 74 return this.threadNamePrefix; 75 } 76 77 82 public void setThreadPriority(int threadPriority) { 83 this.threadPriority = threadPriority; 84 } 85 86 89 public int getThreadPriority() { 90 return this.threadPriority; 91 } 92 93 103 public void setDaemon(boolean daemon) { 104 this.daemon = daemon; 105 } 106 107 110 public boolean isDaemon() { 111 return this.daemon; 112 } 113 114 118 public void setThreadGroupName(String name) { 119 this.threadGroup = new ThreadGroup (name); 120 } 121 122 126 public void setThreadGroup(ThreadGroup threadGroup) { 127 this.threadGroup = threadGroup; 128 } 129 130 134 public ThreadGroup getThreadGroup() { 135 return this.threadGroup; 136 } 137 138 139 146 public Thread createThread(Runnable runnable) { 147 Thread thread = new Thread (getThreadGroup(), runnable, nextThreadName()); 148 thread.setPriority(getThreadPriority()); 149 thread.setDaemon(isDaemon()); 150 return thread; 151 } 152 153 160 protected String nextThreadName() { 161 int threadNumber = 0; 162 synchronized (this.monitor) { 163 this.threadCount++; 164 threadNumber = this.threadCount; 165 } 166 return getThreadNamePrefix() + threadNumber; 167 } 168 169 173 protected String getDefaultThreadNamePrefix() { 174 return ClassUtils.getShortName(getClass()) + "-"; 175 } 176 177 } 178 | Popular Tags |