01: /*
02: * @(#)createThread.java 1.1 05/04/04
03: *
04: * Copyright (c) 2005 Sun Microsystems, Inc. All Rights Reserved.
05: *
06: * See the file "LICENSE.txt" for information on usage and redistribution
07: * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
08: */
09: package org.pnuts.multithread;
10:
11: import pnuts.multithread.ThreadAdapter;
12: import pnuts.lang.Context;
13: import pnuts.lang.PnutsFunction;
14:
15: public class createThread extends PnutsFunction {
16:
17: final static Object[] NO_ARG = new Object[0];
18:
19: public createThread() {
20: super ("createThread");
21: }
22:
23: protected createThread(String name) {
24: super (name);
25: }
26:
27: public boolean defined(int nargs) {
28: return nargs >= 1 && nargs <= 3;
29: }
30:
31: static Thread createThread(PnutsFunction f, int prio,
32: boolean daemon, Context context) {
33: Thread th = new Thread(new PnutsThreadGroup(context),
34: new ThreadAdapter(f, context));
35: th.setPriority(prio);
36: if (daemon) {
37: th.setDaemon(true);
38: }
39: return th;
40: }
41:
42: protected Object exec(Object[] args, Context context) {
43: int nargs = args.length;
44: if (nargs == 1) {
45: PnutsFunction f = (PnutsFunction) args[0];
46: return createThread(f, Thread.NORM_PRIORITY, false, context);
47: } else if (nargs == 2) {
48: PnutsFunction f = (PnutsFunction) args[0];
49: int prio = ((Number) args[1]).intValue();
50: return createThread(f, prio, false, context);
51: } else if (nargs == 3) {
52: PnutsFunction f = (PnutsFunction) args[0];
53: int prio = ((Number) args[1]).intValue();
54: boolean daemon = ((Boolean) args[2]).booleanValue();
55: return createThread(f, prio, daemon, context);
56: } else {
57: undefined(args, context);
58: return null;
59: }
60: }
61:
62: public String toString() {
63: return "function createThread(func {, prio {, daemon }})";
64: }
65: }
|