黑马程序员-----------------线程

来源:互联网 发布:知乎历史版本4.1.0 编辑:程序博客网 时间:2024/06/10 12:41

android培训、java培训、期待与您交流!


线程:是指程序的执行流程,多线程的机制就是可以同时执行多个程序区块,这也就是是多线程存在的意义:并发。

//创建线程具有两种方式:继承Thread类,实现Runnable接口。

启动线程:在类中要想启动线程,应该准备好一下事项

1.定义类继承Thread类。

2.父类Thread类中的run方法。

3.调用线程的start方法,启动线程,调用run方法

代码演示:

//定义Demo类继承Thread类
class Demo extends Thread{public void run(){System.out.println("Demo run!");}}public class ThreadDemo {/** * @param args */public static void main(String[] args) {// TODO Auto-generated method stubDemo d = new Demo();d.start();//开启线程并执行该线程的run方法。d.run();//仅仅是对象调用方法,而线程创建了,并没有运行。}}
重新编写上一段代码,使它可以同时启动多个线程

class Demo extends Thread{private String name;Demo(String name){this.name = name;}public void run(){for (int i = 0; i <5; i++) {System.out.println(name+"--------"+i);}}}public class ThreadDemo {/** * @param args */public static void main(String[] args) {// TODO Auto-generated method stub//为了结果看上去更明显Demo d = new Demo("A");d.start();//开启线程并执行该线程的run方法。Demo d1 = new Demo("B");d1.start();//开启线程并执行该线程的run方法。Demo d2 = new Demo("C");d2.start();//开启线程并执行该线程的run方法。}}


发现运行同一段代码每次的结果都是不同的:
因为多个线程都获取CPU的执行权,CPU执行到谁,谁就运行。明确一点,在某一个时刻,只能有一个程序在运行(多核除外),CPU在做着快速的切换,以达到看上去是同时运行的效果。我们可以形象把多线程的运行行为在互相抢夺CPU的执行权。

这就是多线程的一个特性:随机性,谁抢到谁执行

继承Thread类是线程创建的一种方式,线程创建的另一种方式时通过实现Runnable接口。

通过实现Runnable接口步骤:

1.定义类实现Runnable接口。

2.覆盖Runnable接口中的run方法。
3.通过Thread类建立线程对象。
4.将Runnable接口的子类对象作为实际参数传递给Thread类的构造函数。
5.调用Thread类的start方法开启线程并调用Runnable接口子类的run方法。

代码演示:

class RunnableTest implements Runnable{private String name;RunnableTest(String name){this.name = name;}public void run() {// TODO Auto-generated method stubfor (int i = 0; i <5; i++) {System.out.println(name+"--------"+i);}}}public class RunnableDemo {public static void main(String[] args) {// TODO Auto-generated method stubRunnableTest rt = new RunnableTest("A");//创建一个RunnableTest类对象rtThread t1 = new Thread(rt);//创建线程对象t1t1.start();//启动t1线程RunnableTest rt1 = new RunnableTest("B");//创建一个RunnableTest类对象rt1Thread t2 = new Thread(rt1);//创建线程对象t2t2.start();//启动t2线程//rt.}}


两种方式的区别:

继承Thread:线程代码存放Thread子类run方法中。
实现Runnable:线程代码存放在接口的子类的run方法中


线程生命周期:每一个线程,在其创建和销毁之前,都会具有4中状态之一:新建线程、可执行线程、被挂起线程、销毁线程

四种状态之间的转换可以用流程来表示就很清楚


线程在新创建的时候会进入到新创建线程这里面,通过线程调用start()启动线程,这是线程就正式启动进入可执行线程中,这时可执行的线程调用sleep(millis)或者wait()线程被挂起当,被挂起的线程sleep()结束或调用notify()方法时,线程变为可执行线程,可执行的线程调用stop()时线程结束。

class RTest{public static void show(){for (int i = 0; i <5; i++) {try {Thread.sleep(10);//让线程休眠10毫秒} catch (InterruptedException e) {}System.out.println(Thread.currentThread()+"----"+i);}}}class RunnableTest implements Runnable{public void run() {// TODO Auto-generated method stubRTest.show();}}public class RunnableDemo {public static void main(String[] args) {// TODO Auto-generated method stubRunnableTest rt = new RunnableTest();//创建一个RunnableTest类对象rtThread t1 = new Thread(rt);//创建线程对象t1t1.start();//启动t1线程RunnableTest rt1 = new RunnableTest();//创建一个RunnableTest类对象rt1Thread t2 = new Thread(rt1);//创建线程对象t2t2.start();//启动t2线程//rt.}}
注意:因为sleep()方法会抛出InterruptedException异常,所以sleep()需要写在try---catch间

try {Thread.sleep(10);//让线程休眠10毫秒}
 catch (InterruptedException e) {
}

线程安全问题:
利用同步代码块解决:
synchronized(对象){需要同步的代码 }
同步的前提:
1.必须要有两个或者两个以上的线程。、
2.必须是多个线程使用同一个锁。
同步好处:解决了多线程的安全问题。
弊端:多个线程需要判断锁,较为消耗资源。

//有两个人每人具有300块去银行存钱,每人每次存入100块,多线程的同步方法实现小功能。

class Bank{private int sum;//创建一个对象//Object obj = Object();public void add(int n){synchronized(this){sum = sum +n;try {Thread.sleep(10);} catch (InterruptedException e){}System.out.println("sum="+sum);}}}class Person implements Runnable{private Bank b = new Bank();public void run() {for (int i = 0; i < 3; i++) {b.add(100);}}}public class ThreadDemo1 {public static void main(String[] args) {Person p = new Person();//创建 Person类对象pThread t = new Thread(p);//创建线程对象tThread t2 = new Thread(p);//创建线程对象t2t.start();//启动t线程t2.start();}}

从上面的例子可以看出同步的重要性,同步方法就别面了在处理多个线程共享同一个数据时,一个线程未完成处理某个方法时,另一个线程又闯了进来,这样就很好的提高了线程的安全性。

0 0