I. Tasks and Threads
The specific distinction between threads and tasks in a single section of thinking in Java also shows that tasks and threads are different.
As mentioned above, the task is just a piece of code, a piece of code to achieve your purpose, where this code is written, how to write it does not matter, just because you want Java's multithreading mechanism to recognize and invoke the task you write, so the runnable interface, let your task to implement the interface, Take the work you want to do in the Run method that implements the interface.
So, the task class has been defined, what does the task have to do with threads?
Java threads are used to drive tasks, which means that you have to mount the task on a thread so that the thread can drive the task you define to execute.
Second, define the thread
1. The process of defining a thread is the process of attaching a task to a thread. Thread threads itself does not do anything, it is simply called by a multithreaded mechanism and drives the task assigned to it.
As mentioned in the previous article, the task class definition is as follows:
Public classTaskImplementsRunnable { protected intCountdown = 10; Private Static intTaskcount = 0 ; Private Final intid = taskcount++; PublicTask () {} PublicTask (intCountdown) { This. Countdown =Countdown; } PublicString status () {return"#" +id+ "(" + (Countdown>0?countdown: "task!") +"). "; } @Override Public voidrun () { while(countdown-->0) {System.out.print (status ()); Thread.yield (); } }}
Declare the thread and attach the task to the thread:
New Thread (new Task ());
In this way, the task is attached to the thread, and the following is the call to start the thread:
T.start ();
At this point, the thread declares OK.
Sometimes, I think, is not like the concept of a task and thread separation, at this time just declare the thread, and the Java thread mechanism does not call the thread to run, but also require special calls to achieve multi-threaded execution. But the following piece of code tells me that the start method of the thread class is triggering the multithreading mechanism of Java, so that Java multithreading can invoke the thread
Public Static void Main (string[] args) { new Thread (new Task ()); T.start (); System.out.println ("Waiting for Task");}
The output results are as follows:
for task#0 (9). #0 (8). #0 (7). #0 (6). #0 (5). #0 (4). #0 (3). #0 (2). #0 (1). #0 (task!).
After the output "Waiting for Task" proves that the start () method is finished, the main program is returned immediately and the following statement is executed. And you declare that the T-thread has been called by the Java multithreading mechanism and has driven its task to run.
2. Supplement
To see more thread tasks running, you can use this code
Public Static void Main (string[] args) { for (int i = 0; i < 5; i++) { new Thread (
0>new
Task ()). Start (); } System.out.println ("Waiting for Task"
);}
The output is as follows:
for task#0 (9). #2 (9). #4 (9). #0 (8). #2 (8). #4 (8). #0 (7). #2 (7). #4 (7). #0 (6). #2 (6). #4 (6). #0 (5). #2 (5). #4 (5). #0 (4). #2 (4). #4 (4). #3 (9). #2 (3). #4 (3). #2 (2). #4 (2). #2 (1). #4 (1). #2 (task!). #4 (task!). #1 (9). #0 (3). #0 (2). #0 (1). #0 (task!). #3 (8). #1 (8). #3 (7). #1 (7). #3 (6). #1 (6). #3 (5). #3 (4). #3 (3). #3 (2). #3 (1). #3 (task!). #1 (5). #1 (4). #1 (3). #1 (2). #1 (1). #1 (task!).
The above output shows that the execution of different tasks is mixed together when the threads are swapped out--automatically controlled by the thread scheduler. Different versions of JDK threads are dispatched differently, so the results are not the same.
This involves a problem with the garbage collector, where each thread registers itself, so it does have a reference to it, and the garbage collector cannot erase it until its task exits its run and dies.
Note: The above code is from "Thinking in Java", most of the content is personal understanding and summary, if there are errors please correct me
Java Multithreading--< two > Assign tasks to threads, thread declaration and startup