How to Use runnable in java

Source: Internet
Author: User

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


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.