How to Use runnable in java
1. Class Structure: There are two classes first, one is the test class Main and the other is the Thread class MyThread.
2. MyThread
Package com. test. thread; import java. lang. management. threadInfo; public class MyThread implements Runnable {String name = null; MyThread (String name) {this. name = name ;}@ Overridepublic void run () {// TODO Auto-generated method stubwhile (true) {System. out. println (name + ": output result... ");}}}
Main
Package com. test. thread; public class Main {/*** @ param args */public static void main (String [] args) {// TODO Auto-generated method stubMyThread mya = new MyThread ("Thread A"); Thread testA = new Thread (mya); testA. start (); MyThread myb = new MyThread ("Thread B"); Thread testB = new Thread (myb); testB. start ();}}
3. output results
Vc/Wsbnbo6y4xLavtPrC68jnz8KhozwvcD4KPHA + PHByZSBjbGFzcz0 = "brush: java;"> package com. test. thread; import java. lang. management. threadInfo; public class MyThread implements Runnable {String name = null; MyThread (String name) {this. name = name ;}@ Overridepublic void run () {// TODO Auto-generated method stubwhile (true) {try {Thread. sleep (1000);} catch (InterruptedException e) {// TODO Auto-generated catch blocke. printStackTrace ();} System. out. println (name + ": output result... ");}}}
5. Running result