comparison java/src/java/lang/JThread.d @ 12:bc29606a740c

Added dwt-addons in original directory structure of eclipse.org
author Frank Benoit <benoit@tionex.de>
date Sat, 14 Mar 2009 18:23:29 +0100
parents
children
comparison
equal deleted inserted replaced
11:43904fec5dca 12:bc29606a740c
1 module java.lang.JThread;
2
3 import tango.core.Thread;
4 import java.lang.util;
5 import java.lang.Runnable;
6 import tango.util.log.Trace;
7
8 class JThread {
9
10 private Thread thread;
11 private Runnable runnable;
12
13 private alias ThreadLocal!(JThread) TTLS;
14 private static TTLS tls;
15
16 public static const int MAX_PRIORITY = 10;
17 public static const int MIN_PRIORITY = 1;
18 public static const int NORM_PRIORITY = 5;
19
20 private static TTLS getTls(){
21 if( tls is null ){
22 synchronized( JThread.classinfo ){
23 if( tls is null ){
24 tls = new TTLS();
25 }
26 }
27 }
28 return tls;
29 }
30
31 public this(){
32 thread = new Thread(&internalRun);
33 }
34 public this( void delegate() dg ){
35 thread = new Thread(&internalRun);
36 runnable = dgRunnable( dg );
37 }
38 public this(Runnable runnable){
39 thread = new Thread(&internalRun);
40 this.runnable = runnable;
41 }
42 public this(Runnable runnable, String name){
43 thread = new Thread(&internalRun);
44 this.runnable = runnable;
45 thread.name = name;
46 }
47 public this(String name){
48 thread = new Thread(&internalRun);
49 thread.name = name;
50 }
51
52 public void start(){
53 thread.start();
54 }
55
56 public static JThread currentThread(){
57 auto res = getTls().val();
58 if( res is null ){
59 // no synchronized needed
60 res = new JThread();
61 res.thread = Thread.getThis();
62 getTls().val( res );
63 }
64 assert( res );
65 return res;
66 }
67 public int getPriority() {
68 return (thread.priority-Thread.PRIORITY_MIN) * (MAX_PRIORITY-MIN_PRIORITY) / (Thread.PRIORITY_MAX-Thread.PRIORITY_MIN) + MIN_PRIORITY;
69 }
70 public void setPriority( int newPriority ) {
71 // assert( MIN_PRIORITY < MAX_PRIORITY );
72 // assert( Thread.PRIORITY_MIN < Thread.PRIORITY_MAX );
73 auto scaledPrio = (newPriority-MIN_PRIORITY) * (Thread.PRIORITY_MAX-Thread.PRIORITY_MIN) / (MAX_PRIORITY-MIN_PRIORITY) +Thread.PRIORITY_MIN;
74 Trace.formatln( "JThread.setPriority: scale ({} {} {}) -> ({} {} {})", MIN_PRIORITY, newPriority, MAX_PRIORITY, Thread.PRIORITY_MIN, scaledPrio, Thread.PRIORITY_MAX);
75 // thread.priority( scaledPrio );
76 }
77
78 private void internalRun(){
79 getTls().val( this );
80 if( runnable !is null ){
81 runnable.run();
82 }
83 else {
84 run();
85 }
86 }
87
88 public bool isAlive(){
89 return thread.isRunning();
90 }
91
92 public bool isDaemon() {
93 return thread.isDaemon();
94 }
95
96 public void join(){
97 thread.join();
98 }
99
100 public void setDaemon(bool on) {
101 thread.isDaemon(on);
102 }
103
104 public void setName(String name){
105 thread.name = name;
106 }
107 public String getName(){
108 return thread.name;
109 }
110
111 void interrupt() {
112 implMissing(__FILE__,__LINE__);
113 }
114
115 static bool interrupted() {
116 implMissing(__FILE__,__LINE__);
117 return false;
118 }
119
120 public void run(){
121 // default impl, do nothing
122 }
123 public static void sleep( int time ){
124 Thread.sleep(time/1000.0);
125 }
126 public Thread nativeThread(){
127 assert(thread);
128 return thread;
129 }
130 public override char[] toString(){
131 return "JThread "~thread.name;
132 }
133 public static void yield(){
134 Thread.yield();
135 }
136 }
137