您的位置:首页 > 职场人生

黑马程序员------多线程(No.1)(概述、线程的创建、安全问题、同步锁、同步函数)

2013-09-13 18:29 726 查看
----------------------
ASP.Net+Android+IO开发S、.Net培训、期待与您交流! ----------------------


概述

进程:是一个正在执行中的程序。每一个进程都有一个执行顺序。该顺序是一个执行路径,或者叫一个控制单元。

线程:就是进程中的一个独立的控制单元。线程在控制着进程的执行。

一个进程中至少有一个线程。

Java VM 启动的时候会有一个进程java.exe。

该进程中至少一个线程负责java程序的执行。

而且这个线程运行的代码存在与main方法中。

该线程称之为主线程。

扩展:其实更细节说明jvm,jvm启动不止一个线程,还有负责垃圾回收机制的线程。

1.如何在自定义的代码中,自定义一个线程呢?

//继承Thread类,重写run方法。
class Demo extends Thread{
public void run(){
for(int x = 0;x<30;x++){
System.out.println("demo run.."+x);
}
}
}
class ThreadDemo{
public static void main(String[] args){
Demo d = new Demo();//创建好一个线程
d.start();
for(int x = 0;x<30;x++){
System.out.println("Main run.............."+x);
}
}
}

运行结果,及分析:



为什么要覆盖run方法:

Thread用于描述线程,该类定义了一个功能,用于存储线程要运行的代码。该存储功能就是run方法。

也就是说Thread类中的run方法,用于存储线程要运行的代码

d.run();//仅仅是对象的调用方法,而线程创建了,并没有运行。

d.start();//开启线程,并执行该线程的run方法。

线程的运行状态:



每个线程都有自己默认的名称。

Thread-编号 该编号从0开始。

static Thread currentThread();获取当前线程对象。

getName();获取线程的名称。

设置线程名称:setName或者构造函数。

class Demo extends Thread{
private String msg;
Demo(String msg){
super(msg);
}
public void run(){
for(int x = 0;x<30;x++){
//currentThread();获取当前线程对象。
//getName();获取线程的名称。
System.out.println(Thread.currentThread().getName()+" "+x);
}
}
}
class ThreadDemo{
public static void main(String[] args){
Demo d1 = new Demo("线程一");//创建好一个线程
d1.start();
Demo d2 = new Demo("线程二");
d2.start();
}
}


有关售票的练习程序:

/*
简单的卖票程序
多个窗口买票
*/
class Ticket extends Thread{
private static int tick = 10;
public void run(){
while(true){
if(tick>0){
System.out.println(currentThread().getName()+"sale: "+tick--);
}
}
}
}
public class TicketDemo{
public static void main(String[] args){
Ticket t1 = new Ticket();
Ticket t2 = new Ticket();
Ticket t3 = new Ticket();
Ticket t4 = new Ticket();
t1.start();
t2.start();
t3.start();
t4.start();
}
}


运行结果



创建线程的第二种方式:实现Runnable接口

步骤:

1.定义类实现Runnable接口

2.覆盖Runnable接口中的run方法。将线程要运行的代码放在该run方法中。

3.通过Thread类建立线程对象。

4.将Runnable接口的子类对象作为实际参数传递给Thread类的构造函数。

为什么要将Runnable接口的子类对象传递给Thread的构造函数?

因为,自定义run方法所需要的对象是Runnable接口的子类对象,所以要让线程去指定指定对象的run方法。

5.调用Thread类的start方法开启线程并调用Runnable接口的子类的run方法。

/*
简单的卖票程序
多个窗口买票
*/
//定义类实现Runnable接口
class Ticket implements Runnable{
private static int tick = 10;
//覆盖Runnable接口中的run方法。将线程要运行的代码放在该run方法中
public void run(){
while(true){
if(tick>0){
System.out.println(Thread.currentThread().getName()+"sale: "+tick--);
}
}
}
}
public class TicketDemo{
public static void main(String[] args){
//创建Runnable接口的子类对象
Ticket t = new Ticket();
//通过Thread类建立线程对象。
//将Runnable接口的子类对象作为实际参数传递给Thread类的构造函数。
Thread t1 = new Thread(t);
Thread t2 = new Thread(t);
Thread t3 = new Thread(t);
Thread t4 = new Thread(t);
t1.start();
t2.start();
t3.start();
t4.start();
}
}


实现方式和继承方式有什么区别?

实现方式好处:避免单继承的局限性。在定义线程时,建议使用实现方式。

两种方式的区别:

继承Thread:线程代码存在Thread子类的run方法中。

类实现Runnable:线程代码存在接口的子类的run方法。

多线程的安全问题

如果程序如下

class Ticket implements Runnable {
Object obj = new Object();
private static int tick = 150;
//覆盖Runnable接口中的run方法。将线程要运行的代码放在该run方法中
public void run(){
while(true){
if(tick>0){
try
{
Thread.sleep(10);//sleep10ms;
System.out.println(Thread.currentThread().getName()+"sale: "+tick--);
}
catch (InterruptedException e)
{
}
}

}
}
}
public class TicketDemo{
public static void main(String[] args){
//创建Runnable接口的子类对象
Ticket t = new Ticket();
//通过Thread类建立线程对象。
//将Runnable接口的子类对象作为实际参数传递给Thread类的构造函数。
Thread t1 = new Thread(t);
Thread t2 = new Thread(t);
Thread t3 = new Thread(t);
Thread t4 = new Thread(t);
t1.start();
t2.start();
t3.start();
t4.start();
}
}




问题的原因:当多条语句在操作同一个线程共享数据时,一个线程对多条语句只执行了一部分,还没有执行完,

另一个线程参与进来执行,导致共享数据出错。

解决办法:对多条操作共享数据的语句,只能让一个线程都执行完毕;在执行过程中,其他线程不允许执行。

Java对多线程的安全问题提供了专业的解决方式----同步代码块:

synchronized(对象){

需要被同步的代码

}

解决安全问题后的代码:

/*
问题的原因:当多条语句在操作同一个线程共享数据时,一个线程对多条语句只执行了一部分,还没有执行完,
另一个线程参与进来执行,导致共享数据出错。

解决办法:对多条操作共享数据的语句,只能让一个线程都执行完毕;在执行过程中,其他线程不允许执行。

Java对多线程的安全问题提供了专业的解决方式----同步代码块:
synchronized(对象){
需要被同步的代码
}
*/
class Ticket implements Runnable {
Object obj = new Object();
private static int tick = 150;
//覆盖Runnable接口中的run方法。将线程要运行的代码放在该run方法中
public void run(){
while(true){
synchronized(obj){
if(tick>0){
try
{
Thread.sleep(0);//sleep10ms;
System.out.println(Thread.currentThread().getName()+"sale: "+tick--);
}
catch (InterruptedException e)
{
}
}
}
}
}
}
public class TicketDemo{
public static void main(String[] args){
//创建Runnable接口的子类对象
Ticket t = new Ticket();
//通过Thread类建立线程对象。
//将Runnable接口的子类对象作为实际参数传递给Thread类的构造函数。
Thread t1 = new Thread(t);
Thread t2 = new Thread(t);
Thread t3 = new Thread(t);
Thread t4 = new Thread(t);
t1.start();
t2.start();
t3.start();
t4.start();
}
}



synchronized

同步代码块

Java对多线程的安全问题提供了专业的解决方式----同步代码块:

synchronized(对象){

需要被同步的代码

};

synchronized 同步锁。(就像火车洗手间



对象如同锁,持有锁的线程可以在同步中执行。

没有持有锁的线程即使获取cpu的执行权,也进不去,因为没有锁。

同步的前提:

1.必须要有两个或者两个以上的线程。

2.必须是多个线程使用同一个锁。

3.必须保证同步中只能有一个线程在运行。

好处:解决了多线程的安全问题。

弊端:多个线程每次都要判断锁,较为消耗资源咯(是在允许范围内的)。

同步函数

需求:

银行有一个金库。两个储户分别存300元,每次100,存3次。

目的:找到改程序的安全问题,以及如何解决?

/*
需求:
银行有一个金库。两个储户分别存300元,每次100,存3次。

目的:找到改程序的安全问题,以及如何解决?
*/
class Bank{
private int sum;
public void add(int n){
try{
sum = sum+n;
Thread.sleep(10);
System.out.println("sum="+sum);
}catch(Exception e){}
}
}
class Cus implements Runnable{
private Bank b = new Bank();
public void run(){
for(int x=0;x<3;x++){
b.add(100);
}
}
}
class BankDemo{
public static void main(String[] args){
Cus cus = new Cus();
Thread t1 = new Thread(cus);
Thread t2 = new Thread(cus);
t1.start();
t2.start();
}
}

输出结果出现问题:



class Bank{
private int sum;
//	Object obj = new Object();
public synchronized void add(int n){
//	synchronized(obj){
sum = sum+n;
try{
Thread.sleep(10);
}catch(Exception e){}
System.out.println("sum="+sum);
//	}
}
}
class Cus implements Runnable{
private Bank b = new Bank();
public void run(){
for(int x=0;x<3;x++){
b.add(100);
}
}
}
class BankDemo{
public static void main(String[] args){
Cus cus = new Cus();
Thread t1 = new Thread(cus);
Thread t2 = new Thread(cus);
t1.start();
t2.start();
}
}

输出正确结果:



同步函数用的是哪一个锁呢?

函数需要被对象调用。那么函数都有一个所属对象引用。就是this。

所以同步函数使用的锁是this。

/*
同步函数用的是哪一个锁呢?
函数需要被对象调用。那么函数都有一个所属对象引用。就是this。
所以同步函数使用的锁是this。
*/
class Ticket implements Runnable {
Object obj = new Object();
private static int tick = 150;
//覆盖Runnable接口中的run方法。将线程要运行的代码放在该run方法中
public void run(){
while(true){
//将需要被锁的代码抽取成功能 public synchronized void show()
this.show();
}
}
//定义同步锁功能,该锁是this。
public synchronized void show(){
if(tick>0){
try{
Thread.sleep(10);//sleep10ms;
System.out.println(Thread.currentThread().getName()+"sale: "+tick--);
}
catch (InterruptedException e){
}
}
}
}
public class ThisLockDemo{
public static void main(String[] args){
//创建Runnable接口的子类对象
Ticket t = new Ticket();
//通过Thread类建立线程对象。
//将Runnable接口的子类对象作为实际参数传递给Thread类的构造函数。
Thread t1 = new Thread(t);
Thread t2 = new Thread(t);
Thread t3 = new Thread(t);
Thread t4 = new Thread(t);
t1.start();
t2.start();
t3.start();
t4.start();
}
}


如果同步函数被静态修饰后,使用的锁是什么呢?

通过验证,发现不再是this。因为静态方法中也不可以定义this。

静态近内存时,内存中没有本类对象,但是一定有笨类对应的字节码文件对象。

类名.class 该对象的类型是Class。

静态的同步方法,使用的锁是该方法所在类的字节码文件对象,类名.class。

----------------------
ASP.Net+Android+IOS开发、.Net培训、期待与您交流! ----------------------
详情请查看:http://edu.csdn.net
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐