实现多线程的两种方式:
/*
* 【1.1进程和多线程的概念及线程的优点】:
* 【进程】:
* 1.进程:进程是操作系统结构的基础,是一次程序的执行;是一个程序及其数据在处理机上顺序执行是所发生的活动, 是程序在一个数据集合上运行的过程,他是系统进行资源分配和调度的一个独立单位。
* 【多线程】:
* 1.单任务的特点就是排队执行,也就是同步,就像cmd命令一样必须一行一行的执行。
* 这就是单任务环境的缺点,及CPU利用率大幅降低。
* 2.多任务中,CPU可以在任务1和任务n中来回切换,使任务n不必等太久,系统的运行效率提升。
* 这就是多线程的技术优点,使用多线程也就是在使用异步。
* 3.多线程是异步的,线程被调用的时机是随机的。
*
* 【使用多线程】:
* 1.实现多线程主要有两种方式:一种是继承Thread类,另一种是实现runnable接口。
* 2.Thread的结构 :public class Thread implements Runnable
*
*/
package cn.hncu.lang.thread_;
/**
* 项目名:多线程说明
* 时间 :2017-9-17 上午11:51:04
*/
public class ThreadExplain {
/*
* 第一个线程【main线程】:
* 1.调用public static void main()方法的线程是在后台默默执行的,它是由JVM创建的。
* 2.执行结果为main,这个main是一个main线程,main线程执行了main方法中的代码。
*/
public static void main(String[] args) {
System.out.println(Thread.currentThread().getName());//main
}
}
/**
* 【继承Thread类】:
*
* 1.MyThread类程序执行的结果是:
* 运行结束…
* MyThread…..
* 2.MyThread类中的run方法执行的时间比较晚,这一情况说明:
* 在使用多线程技术时,代码的运行结果与代码执行顺序或调用顺序是无关的。
* 3.执行start()方法的顺序不代表线程启动的顺序。
* 4.线程的调用的随机性。
*/
class MyThread extends Thread{
@Override
public void run() {
super.run();
System.out.println("myThread.....");
}
public static void main(String[] args) {
MyThread t = new MyThread();
t.start();
System.out.println("运行结束...");
//【多次调用start会出现】java.lang.IllegalThreadStateException
//t.start();
}
}
/*
* 【演示线程的随机性】:
*/
class RandomThread extends Thread{
@Override
public void run() {
super.run();
for(int i =0; i < 10; i++){
int time = (int) (Math.random()*100);
try {
Thread.sleep(time);
System.out.println("run = "+Thread.currentThread().getName());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
/*
* 【测试线程的随机性】:
*/
class TestRandomThread{
public static void main(String[] args) {
RandomThread thread = new RandomThread();
thread.setName("randomThread");
thread.start();
for(int i =0; i < 10; i++){
int time = (int) (Math.random()*100);
try {
Thread.sleep(time);
System.out.println("main = "+Thread.currentThread().getName());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
/*
* 【测试start方法的顺序和线程启动的顺序】:
* 1.由此可得:start()方法的调用顺序和线程的启动顺序是无关的。
*/
class TestStartThread extends Thread{
private int i;
public TestStartThread(int i) {
this.i = i;
}
@Override
public void run() {
System.out.println(i);
}
public static void main(String[] args) {
TestStartThread t1 = new TestStartThread(1);
TestStartThread t2 = new TestStartThread(2);
TestStartThread t3 = new TestStartThread(3);
TestStartThread t4 = new TestStartThread(4);
TestStartThread t5 = new TestStartThread(5);
TestStartThread t6 = new TestStartThread(6);
TestStartThread t7 = new TestStartThread(7);
TestStartThread t8 = new TestStartThread(8);
t1.start();
t2.start();
t3.start();
t4.start();
t5.start();
t6.start();
t7.start();
t8.start();
}
}
/**
* 【实现runnable接口】:
*
* 1.如果欲创建的线程类已经有一个父类了,这时就不能再继承自Thread类了,
* 因为Java不支持多继承,所以就需要实现Runnable接口来应对这样的情况。
* 2.如何使用runnable接口实现类?
* 3.利用Thread类的构造方法:Thread(Runnable target) 分配新的 Thread 对象。
*
* 4.实现runnable接口的好处:
* 构造函数Thread(Runnable target) 不光可以传入Runnable接口的对象,还可以传入一个Thread类的对象,
* 这样做可以将一个Thread对象中的run()方法交由其他的线程进行调用。
*
*/
class MyRunnable implements Runnable{
@Override
public void run() {
System.out.println("运行中.....");
}
public static void main(String[] args) {
//1.创建Runnable接口实现类对象
MyRunnable myRunnable = new MyRunnable();
//2.使用Thread构造方法:Thread(Runnable target)创建对象
Thread t = new Thread(myRunnable);
//3.启动线程
t.start();
//4.Thread构造方法传入Thread对象
Thread t2 = new Thread(t);
t2.start();//依然可以运行
}
}