Java多线程-Thread&Runnable的关系
Posted starstarstar
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java多线程-Thread&Runnable的关系相关的知识,希望对你有一定的参考价值。
Thread创建多线程:
MyThread.class package com.test.interview; public class MyThread extends Thread private String name; public MyThread(String name) this.name = name; @Override public void run() for (int i = 0; i < 10; i++) System.out.println("thread start:" + this.name + ",i=" + i);
ThreadDemo.class package com.test.interview; public class ThreadDemo public static void main(String[] args) MyThread mt = new MyThread("thread1"); MyThread mt2 = new MyThread("thread2"); MyThread mt3 = new MyThread("thread3"); mt.start(); mt2.start(); mt3.start();
Runnable创建多线程:
RunnableDemo.class package com.test.interview; public class RunnableDemo public static void main(String[] args) MyRunnable mr1 = new MyRunnable("Runnable1"); MyRunnable mr2 = new MyRunnable("Runnable2"); MyRunnable mr3 = new MyRunnable("Runnable3"); Thread t1 = new Thread(mr1); Thread t2 = new Thread(mr2); Thread t3 = new Thread(mr3); t1.start(); t2.start(); t3.start();
MyRunnable.class package com.test.interview; public class MyRunnable implements Runnable private String name; public MyRunnable(String name) this.name = name; @Override public void run() for (int i = 0; i < 10; i++) System.out.println("thread start:" + this.name + ",i=" + i);
以上是关于Java多线程-Thread&Runnable的关系的主要内容,如果未能解决你的问题,请参考以下文章
java多线程同步以及线程间通信详解&消费者生产者模式&死锁&Thread.join()(多线程编程之二)