[Java] Simple implementation of multi-threaded

xiaoxiao2021-03-06  82

There are two ways to implement multithreaded methods in Java: Thread class, Runnable interface. The following code briefly introduces these two methods.

THREAD Class: Inheriting the THREAD class and re-implementing the Run () method

Public class mythread extends thread {int count; string name;

Public mythread (string name, int CT) {this.name = name; count = ct; system.out.println ("Create thread:" name);

Public void Run () {for (int i = 1; i

Public static void main (string args []) {New Mythread ("Thread1", 5) .Start (); New Mythread ("Thread2", 3) .Start (); New Mythread ("Thread3", 4) .start ();

Runnable interface: A Run () method is defined in RunNable, and this method must be defined in the object of runnable. When a new THRead object is created, the Runnable object is implemented as a parameter, and the Tread object will call the RUN () method.

Public class myrunnable imports runnable {

INT country;

String name;

Public myrunnable (string name, int ct) {this.name = name; count = ct; system.out.println ("Create Thread:" Name);

Public void Run () {for (int i = 1; i

Public static void main (string args []) {new trread (New Myrunnable ("Thread1", 5)). start (); New Thread (New MyRunnable ("Thread2", 3)). start (); New Thread New Myrunnable ("Thread3", 4)). Start ();}}

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

New Post(0)