Java Authentication: javarunnable threads Write interface code. How can javarunnable threads better adapt to the current programming environment? Let's take a look at how we can better carry out the relevant environment. I hope the following article is helpful to you. The javarunnable thread interface has only one method run (), and we declare our class to implement the Runnable interface and provide this method.
This part of the task is accomplished by writing our thread code into it. But the Runnable interface does not have any support for threading, and we must also create an instance of the thread class, which is implemented by the thread class's constructor public thread (Runnable target).
Here is an example:
1.public class MyThread implements Runnable
0.9
3.int count= 1, number;
4.public MyThread (int num)
5.{
6.numnumber = num;
7.SYSTEM.OUT.PRINTLN ("Create thread" + number);
8.}
9.public void Run ()
10.{
11.while (True)
12.{
13.system.out.println
14. ("Thread" + number + ": Count" + count);
15.if (++count== 6) return;
16.}
17.}
18.public static void Main (String args[])
19.{
20.for (int i = 0; i〈5;
21.i++) New Thread (New MyThread (i+1)). Start ();
22.}
23.}
Strictly speaking, it is possible to create an instance of the thread subclass, but it must be noted that the subclass must not overwrite the Run method of the thread class, otherwise the thread will execute the Run method of the subclass, rather than the run method of the class that we use to implement the Java Runnable threading interface. Let's try this one.TOEFL Answers www.jszdsy.com
Using the Runnable interface to enable multithreading allows us to accommodate all of the code in a class, and the downside is that we can only use a set of code, and if you want to create multiple threads and have different code for each thread, you still have to create the additional class, if that's the case, In most cases, it might not be as compact to inherit the Thread directly from multiple classes.
In summary, the two methods are different, we can use flexibly. Let's take a look at some of the problems in multithreaded use.
Four states of Java runnable threadswww.yzyxedu.com
1. New status: Thread has been created but not yet executed (start () has not yet been called).
2. Executable state: Threads can execute, although not necessarily executing. The CPU time may be assigned to the thread at any time, allowing it to execute.
3. Death status: Normally the run () return causes the thread to die. Calling Stop () or destroy () also has the same effect, but is not recommended, the former will produce an exception, the latter is forced to terminate, will not release the lock.
4. Blocking status: Threads are not allocated CPU time and cannot be executed.
Java Authentication: javarunnable Thread Writing interface code