Two methods for realizing a thread

xiaoxiao2021-03-06  93

Two methods for realizing a thread

Shan brother http://blog.9cbs.net

Java completes multithreading through the Java.lang.Thread class. Through the same example, we look at the two creation methods of threads, and how to operate

One is a subclass of Thread, and the method of overloading the Thread class Run Extend java.lang.thread and Override the Run Method.

Public class mythread extends thread {public void run () {// Because the default Run () method does nothing, you must create Thread subclasses and overload Run () to complete useful work. For (int count = 1, row = 1; ROW <20; Row , Count ) {for (int i = 0; i

There are two methods, A, B.

Public static void main (String [] args) {mythread mt = new mythread (); // a mt.start (); // a thread mythread = new thread ()); // b mythread.start ( ); // b for (int i = 0; i <500; i ) {system.out.println (i);}}

Another way is to declare a class that implements the Runnable interface. Then implement method Run. Implement java.lang.Runnable and import the run method.

// public class mythread extends thread {public class mythread implements runnable {public void run () {for (int count = 1, row = 1; row <20; row , count ) {for (int i = 0; i

There is only one method B at runtime.

Public static void main (String [] args) {// mythread mt = new mythread (); // mt.start (); thread mythread = new thread (); mythread.start (); for (int); I = 0; i <500; i ) {system.out.println (i);}}

转载请注明原文地址:https://www.9cbs.com/read-122608.html

New Post(0)