thread类
- package hu.th;
- public class MyThread {
- public MyThread(){
- new MyTh().start();
- new MyTh().start();
- new MyTh().start();
- }
- public static void main(String[] args) {
- new MyThread();
- }
- class MyTh extends Thread{
- @Override
- public void run() {
- for(int i=0;i<100;i++){
- System.out.println(Thread.currentThread().getName()+"--"+i);
- try {
- Thread.sleep(100);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }
- }
- }
Runnable接口
- package hu.th;
- public class MyThread {
- public MyThread(){
- new Thread(new MyRun()).start();
- new Thread(new MyRun()).start();
- new Thread(new MyRun()).start();
- }
- public static void main(String[] args) {
- new MyThread();
- }
- class MyRun implements Runnable{
- @Override
- public void run() {
- for(int i=0;i<100;i++){
- System.out.println(Thread.currentThread().getName()+"--"+i);
- try {
- Thread.sleep(100);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }
- }
- }