小伙伴们,我们下面来看一段代码的例子吧,呼呼(~ o ~)~zZ
package net.mindview.util;
//第一步、实现Runnable接口
class MyThreadRunning implements Runnable
{
//第二步、重写run方法
public void run() {
for (int i = 0; i <= 3; i++) {
System.out.println(Thread.currentThread().getName());
}
}
}
public class MyThread {
public static void main(String[] args) {
//第三步、实例化自定义的线程类对象
MyThreadRunning mtr1 = new MyThreadRunning();
MyThreadRunning mtr2 = new MyThreadRunning();
MyThreadRunning mtr3 = new MyThreadRunning();
//第四步、实例化一个Thread类对象并将自定义的线程类对象作为参数传入,后面一个参数为线程名
Thread thread1 = new Thread(mtr1, "Thread 1 is running");
Thread thread2 = new Thread(mtr2, "Thread 2 is running");
Thread thread3 = new Thread(mtr3, "Thread 3 is running");
thread1.setPriority(Thread.MAX_PRIORITY);
thread2.setPriority(Thread.MIN_PRIORITY);
thread3.setPriority(Thread.NORM_PRIORITY);
//第五步、调用start方法启动线程
thread1.start();
thread2.start();
thread3.start();
}
}运行结果:package net.mindview.util;
//第一步、实现Runnable接口
class MyThreadRunning implements Runnable
{
//第二步、重写run方法
public void run() {
for (int i = 0; i <= 3; i++) {
System.out.println(Thread.currentThread().getName());
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
public class MyThread {
public static void main(String[] args) {
//第三步、实例化自定义的线程类对象
MyThreadRunning mtr1 = new MyThreadRunning();
MyThreadRunning mtr2 = new MyThreadRunning();
MyThreadRunning mtr3 = new MyThreadRunning();
//第四步、实例化一个Thread类对象并将自定义的线程类对象作为参数传入,后面一个参数为线程名
Thread thread1 = new Thread(mtr1, "Thread 1 is running");
Thread thread2 = new Thread(mtr2, "Thread 2 is running");
Thread thread3 = new Thread(mtr3, "Thread 3 is running");
//第五步、调用start方法启动线程
thread1.start();
thread2.start();
thread3.start();
}
}运行结果:package net.mindview.util;
//第一步、实现Runnable接口
class MyThreadRunning1 implements Runnable
{
//第二步、重写run方法
public void run() {
for (int i = 0; i <= 3; i++) {
System.out.println(Thread.currentThread().getName());
}
}
}
//第一步、实现Runnable接口
class MyThreadRunning2 implements Runnable
{
//第二步、重写run方法
public void run() {
for (int i = 0; i <= 3; i++) {
System.out.println(Thread.currentThread().getName());
Thread.yield();
}
}
}
public class MyThread {
public static void main(String[] args) {
//第三步、实例化自定义的线程类对象
MyThreadRunning1 mtr1 = new MyThreadRunning1();
MyThreadRunning2 mtr2 = new MyThreadRunning2();
//第四步、实例化一个Thread类对象并将自定义的线程类对象作为参数传入,后面一个参数为线程名
Thread thread1 = new Thread(mtr1, "Thread 1 is running");
Thread thread2 = new Thread(mtr2, "Thread 2 is running");
//第五步、调用start方法启动线程
thread1.start();
thread2.start();
}
}运行效果: 如果不加yield的可能的运行结果:package net.mindview.util;
//第一步、继承Thread类
class MyThreadRunning extends Thread
{
//构造函数
public MyThreadRunning() {
super("My Thread");
}
//第二步、重写run方法
public void run() {
for (int i = 0; i <= 3; i++) {
System.out.println("新建一个线程" + getName()+i);
try {
sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
public class MyThread {
public static void main(String[] args) {
//第三步、实例化自定义的线程类对象
MyThreadRunning mtr = new MyThreadRunning();
//第四步、调用start方法从而启动run方法
mtr.start();
try {
mtr.join();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("mtr你执行完了是吧?我是主线程,我要来打印了。");
}
}运行结果:package net.mindview.util;
//第一步、继承Thread类
public class MyThread {
public static void main(String[] args) {
Thread t1 = new MyCommon();
Thread t2 = new Thread(new MyDaemon());
t2.setDaemon(true); //设置为守护线程
t2.start();
t1.start();
}
}
class MyCommon extends Thread {
public void run() {
for (int i = 0; i < 5; i++) {
System.out.println("线程1第" + i + "次执行!");
try {
Thread.sleep(7);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
class MyDaemon implements Runnable {
public void run() {
for (long i = 0; i < 9999999L; i++) {
System.out.println("后台线程第" + i + "次执行!");
try {
Thread.sleep(7);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}运行结果:原文地址:http://blog.csdn.net/xiaxia__/article/details/44916783