User since: Fri Aug 17 2001 at 22:27
Sherlock is Level 9 (Sage) with 2043 experience points!
Last logged in: Wed Nov 17 2004 at 16:22 (303 weeks ago)
Number of posts: 155
Notes to crazy:
because they are controlled by your torso, and cannot operate without it, that is, they are dependent, or children, of your body.
This isn't quite accurate in Java. True, you need to start a thread within another program but, once you've begun a thread, it is not necessarily dependent upon the program that spawned it. Take the following code:
1: // Class ThreadTest (Driver)
2:
3: import java.lang.Thread;
4:
5: public class ThreadTest
6: {
7:
8: public ThreadTest()
9: {
10: super();
11: }
12:
13: public static void main(java.lang.String[] args)
14: {
15: Thread t = new Thread(new ExampleThread());
16: t.start();
17: System.out.println("Main Done");
18: }
19: }
20:
21: // Class ExampleThread (implements Runnable)
22:
23: import java.lang.Thread;
24:
25: public class ExampleThread implements Runnable
26: {
27: public ExampleThread()
28: {
29: super();
30: }
31:
32: public void run()
33: {
34: int i = 0;
35: while (i < 20)
36: {
37: System.out.println(i++);
38:
39: try {
40: Thread.sleep(1000);
41: }
42: catch (InterruptedException e)
43: {
44: break;
45: }
46: }
47: System.out.println("Completed");
48: }
49: }
50:
51: // Output:
52: 0
53: Main Done
54: 1
55: 2
56: 3
57: 4
58: 5
59: 6
60: 7
61: 8
62: 9
63: 10
64: 11
65: 12
66: 13
67: 14
68: 15
69: 16
70: 17
71: 18
72: 19
73: Completed
You'll notice that, even though the main method that initially spawned the thread has completed execution, the thread it spawned is able to complete it's own execution (it is now being executed independently from the spawning thread). If you add one line to the main method, however, you change the output drastically:
1: Thread t = new Thread(new ExampleThread());
2: t.setDaemon(true);
3: t.start();
4: System.out.println("Main Done");
5:
6: // Output
7: 0
8: Main Done
In this case, because we're making the thread a "daemon" thread, it is dependent upon the spawning thread. When that application completes, the thread it spawned dies.
To summarize, I think your statement about threads being dependent upon the application that spawned them is a bit misleading. ;)
In your pseudocode, you're using sleep(1) to represent sleeping by 1 second and, later in your real code, you use sleep(1) again, but the argument to Thread's sleep method is actually milliseconds. In your pseudocode, you comment that you mean to sleep for 1 second, but in your code, you'll only be sleeping for 1 millisecond. This also might be a bit misleading. You may also want to mention that sleep is a static method of the Thread class (and therefore usable by an object that implements Runnable).
I didn't see any areas where you were going to discuss how to stop threads. Most people (myself included) are inclined to use the stop method within the Thread class in order to stop the threads, but that is unsafe and now deprecated. A discussion of the "proper" way to terminate a thread would probably be useful.
In my last tutorial (on JDBC), I used a lot of links to the Java 2 API. I got a lot of comments from people saying that they really liked having those links available so that they could easily find greater detail on anything I had quoted in the tutorial. You might find this especially useful when it comes to the Thread class, the Runnable interface, and any methods of them, such as Runnable.run(), Thread.start(), and Thread.sleep(int ms).
Hopefully, this is some help to you. I haven't had a chance to look at the entire thing in great detail - let me know when you have some more and I'll be happy to look at it again.
|
|
|
Tick tock
|
JavaJunkie time is : Tue Sep 7 15:19:47 CEST 2010
|
|
|
|
|
|
|
[*]
Other Users
|
|
ain't nobody here
|
|
|
|