黑马程序员——多线程

---------------------- 黑马程序员 Android培训、期待与您交流! ----------------------

进程的概述

 * 进程:是一个正在执行中的程序。

 *      每一个进程执行都有一个执行顺序,该顺序是一个执行路径,或者叫一个执行单元

 *     

 * 线程:就是进程中的一个独立单元。

 *      线程控制着进程的执行。

 *     

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

 *     

 * Java  Jvm(虚拟机)启动的时候会有一个进程java.exe

 *

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

 * 而且这个线程的执行代码存在于main方法中。

 * 该线程称之为:主线程

 *

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


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

 *

 * 通过对API查找,Java已经提供了对线程这类事物的描述。

 *

 * 创建线程的第一种方式:继承Thread类

 * 步骤:

 * 1,定义类继承hread

 * 2,覆写Thread类中的run方法

 *    目的:将自定义代码存储在run方法中。让线程运行。

 *

 * 3,调用线程的start方法(该方法有两个作用:)---启动线程-调用run方法

 *

 * 总结:Run方法与start方法的区别:

 *     Run:是让自定义代码存储,让线程运行。

 *     start:启运线程调用方式,调用Run方法

 *

 * 发现运行结果,每一次都不一样。

 * 因为多个线程,都在获取CPU的执行权,CPU执行到谁,谁就运行。

 * 明确一点,在某一时刻,只能有一个程序在运行,(多核除外)

 * CPU在做着快速的切换,以达到看上去是同时运行的效果。

 *

 * 我们可以形象的把多线程的运行,形容为:在互相抢CPU的资源,这就是多线程的特性:随机性。

 * 就是谁抢到,谁运行,至于执行多长时间,CPU说了算。

 */

class DemoThread extends Thread
{
	public void run()
	{
		for(int i=0;i<60;i++)
		{
			System.out.println("Hello World"+i);
		}
	}
}
public class _08多线程 {
	
	public static void main(String[]args)
	{
		DemoThread d = new DemoThread();
		d.start();//开启线程,并执行该线程的run方法
		
		//d.run();仅仅是对象调用方法,而线程创建了,并没有运行。
		
		for(int i=0;i<70;i++)
		{
			System.out.println("Run....."+i);
		}
	}
	

}


线程stars与run的特点

/*

 * 创建线程的第一种方式:继承Thread

 * 步骤:

 * 1,定义类继承hread

 * 2,覆写Thread类中的run方法

 *    目的:将自定义代码存储在run方法中。让线程运行。

 *

 * 3,调用线程的start方法(该方法有两个作用:)---启动线程-调用run方法

 *

 * ---------------------------------------

 * 为什么要覆盖Run方法呢、

 * Thread类用于描述线程。

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

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

 *

 */

class DemoThreadOne extends Thread
{
	public void run()
	{
		for(int i=0;i<60;i++)
		{
			System.out.println("Hello World"+i);
		}
	}
}

public class _09创建线程run和start的特点 {

	
	public static void main(String[]args)
	{
		DemoThread d = new DemoThread();
		
		d.start();//开启线程,并执行该线程的run方法
		
		//d.run();仅仅是对象调用方法,而线程创建了,并没有运行。
		
		for(int i=0;i<70;i++)
		{
			System.out.println("Run....."+i);
		}
	}
	
}

线程运行状态



创建线程的第二种方式 继承Runnable接口

/*

 * 示例:卖票程序

 * 多个窗口同时卖票。

 * ---------------------------------------------------------------------------------

 * 注意:Thread.sleep(10);这里出现的异常不能抛,因为它是Runnable接口的run方法里面的异常,

 *      所以,如果接口里面的方法抛异常,程序就会运行不了

 *---------------------------------------------------------------------------------

 * 创建线程的第二种方式:

 * 步骤:

 * 1,定义类,实现Runnable接口

 * 2,覆盖Runnable接口中的run方法

 *    将线程要运行的代码存放在该run方法中。

 *

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

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

 *    为什么要将Runnable接口的子类对象传递给Thread的构造函数。(因为它有一个函数是接收Runnalbe的)

 *    因为,自定义的run方法所属的对象是Runnable接口的子类对象。

 *     所以要让线程去指定对象的run方法.就必须明确该Run方法所属的对象。

 *    

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

 *

 *  继承Thread 和 实现Runnable区别

 * 实现方式好处:避免了单线程的局限性,

 * 在定义线程时,建议使用(实现方式)Runnable线程方式

 *

区别:

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

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

 *

 */

class Tickt implements Runnable//extends Thread
{
	private int tick = 100;
	public void run()
	{
		while(true)
		{
			if(tick>0)
			{
				System.out.println(Thread.currentThread().getName()+"卖票:"+tick--);
				//"卖票:"+tick--    等于   tick,tick--    
			}
		}
	}
}
public class _11多线程_售票 {
	
	public static void main(String[]args)
	{
		Tickt t = new Tickt();
		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();
		/*
		Tickt t1 = new Tickt();
		Tickt t2 = new Tickt();
		Tickt t3 = new Tickt();
		Tickt t4 = new Tickt();
		t1.start();
		t2.start();
		t3.start();
		t4.start();
		*/
	}

}

线程同步

l        同步的前提:

1.        同步需要两个或者两个以上的线程。

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

3.        未满足这两个条件,不能称其为同步。

l        同步的弊端:

当线程相当多时,因为每个线程都会去判断 同步上的锁,这是很耗费资源的,无形 中会降低程序的运行效率。


/*

 * 银行:有一个金库

 * 有两个储户分别存300元,每次存100,存3

 *

 * 目的:该程序是否有安全问题《如果有,如何解决?》

 *

 * 如何找问题:

 * 1,明确哪些代码是多线程运行代码

 * 2,明确共享数据

 * 3,明确多线程运行代码中哪些语句是操作共享数据的。、

 *

 * 同步数据方法,可以放在函数上

 */

class Bank
{
	private int sum;
	public synchronized void Add(int num)
	{
		sum = sum + num;
		System.out.println(Thread.currentThread().getName()+"sum:"+sum);
	}
}
class Cus implements Runnable
{
	private Bank b = new Bank();
	public void run()
	{
		for(int x=0;x<3;x++)
		{
			b.Add(100);
		}
	}
}

public class _12多线程_同步函数 {
	
	public static void main(String[]args)
	{
		Cus c = new Cus();
		Thread t1 = new Thread(c);
		Thread t2 = new Thread(c);
		t1.start();
		t2.start();
	}

}


同步代码块

/*

 

通过分析,发现,打印出0-1-2等错票。

 

多线程的运行出现了安全问题。

 

问题的原因:

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

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

 

解决办法:

    对多条操作共享数据的语句,只能让一个线程都执行完。在执行过程中,其他线程不可以参与执行。

 

 

 

Java对于多线程的安全问题提供了专业的解决方式。

 

就是同步代码块。

 

synchronized(对象)

{

    需要被同步的代码

 

}

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

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

 

火车上的卫生间---经典。

 

同步的前提:

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

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

 

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

 

 

同步的利与弊

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

 

弊端:多个线程需要判断锁,较为消耗资源,

 

*/

 

class Ticket implements Runnable
{
	private  int tick = 1000;
	Object obj = new Object();
	public void run()
	{
		while(true)
		{
			synchronized(obj)
			{
				if(tick>0)
				{
					//try{Thread.sleep(10);}catch(Exception e){}
					System.out.println(Thread.currentThread().getName()+"....sale : "+ tick--);
				}
			}
		}
	}
}


class  _11_同步代码块
{
	public static void main(String[] args) 
	{

		Ticket t = new Ticket();

		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.

 *

 * 通过该程序进行验证

 *

 * 使用两个线程来卖票

 * 一个线程在同步代码块中。

 * 一个线程在同步函数中。

 * 都在执行卖票动物。

 * ------------------------------------------

 * 重点:

 * 先让主线程停止10毫秒,当程序执行到t2后,执行一会儿,主线程突然醒了,然后执行主线程

 *

 *---------------------------------------------------------------------------

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

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

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

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

 *

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

 */

class DemoTecket implements Runnable
{
	private int ticket = 100;
	Object obj = new Object();
	boolean flag = true;

	public void run()
	{
		if(flag)
		{
			while(true)
			{
				//如果show方法被静态修饰,这里调用的对象是DemoTicket.class
				synchronized(this)//因为run方法调用的show()方法,show方法调用的是this.所以这里是this
				{
					if(ticket>0)
					{
						try{Thread.sleep(10);}catch(Exception e){}
						System.out.println(Thread.currentThread().getName()+"...同步售票:"+ticket--);
					}
				}
			}
		}
		else
		{
			while(true)
			{
				show();
			}
		}
	}

		public synchronized void show()
		{
			if(ticket>0)
			{
				try{Thread.sleep(10);}catch(Exception e){}
				System.out.println(Thread.currentThread().getName()+"...函数售票:"+ticket--);
			}
		}
	
	
}
public class _13多线程_同步函数的锁是this {
	
	public static void main(String[]args)
	{
		DemoTecket dt = new DemoTecket();
		
		Thread t1 = new Thread(dt);
		Thread t2 = new Thread(dt);
		Thread t3 = new Thread(dt);
		Thread t4 = new Thread(dt);
		t1.start();
		//重点:先让主线程停止10毫秒,当程序执行到t2后,执行一会儿,主线程突然醒了,然后执行主线程
		try{Thread.sleep(10);}catch(Exception e){}
		dt.flag=false;
		t2.start();
		//t3.start();
		//t4.start();
		
	}

}


静态同步函数的锁是Class对象

/*

 * 用两个线程进行:卖票

 *

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

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

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

 *

 * 如何找问题:

 * 1,明确哪些代码是多线程运行代码

 * 2,明确共享数据

 * 3,明确多线程运行代码中哪些语句是操作共享数据的。、

 *

 *--------------------------------------------------------

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

 *

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

 *

 * 我们可以这样想,

 * 静态进入内存的时候,是没有本类对象的,首先加载的是所以一定会有该类对应的字节码文件对象(类名.class)

 * 类进入内存,会建立一个字节码文件对象。

 *

 * 由此推断-->被静态修饰的同步方法锁:用的是class(类)

 *

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

 *--------------------------------------------------------------

 */

class TicketDemo implements Runnable
{
	//将票私有500张票
	private static int tick = 100;
	
	//定义一个标记,用来调用两个线程
	boolean flag = true;
	
	//覆盖runnable中的run方法
	public void run()
	{
		if(flag)
		{
			//打印车票
			while(true)
			{
				
				//如果show方法被静态修饰,这里调用的对象是Ticket.class
				synchronized(TicketDemo.class)//因为run方法调用的show()方法,show方法调用的是this.所以这里是this
				{
					if(tick>0)
					{
						try{Thread.sleep(10);}catch(Exception e){}
						System.out.println(Thread.currentThread().getName()+"同步售票"+tick--);
					}
				}
			}
		}
		else
			while(true)
				show();
	}
	
	//对操作共享数据的代码块进行封装
	public static synchronized void show()
	{
		if(tick>0)
		{
			try{Thread.sleep(10);}catch(Exception e){}
			//currentThread();获取线程名称
			System.out.println(Thread.currentThread().getName()+"..同步函数售票.."+tick--);
		}
	}
	
}
public class _13多线程_静态同步函数的锁是class对象 {
	
	public static void main(String[]args)
	{
		TicketDemo t = new TicketDemo();
		
		//创建线程
		Thread t1 = new Thread(t);
		Thread t2 = new Thread(t);
		
		t1.start();
		
		
		//当主函数执行到这儿时,让它停留10秒;原因是,main主函数线程会瞬间将两个线程开启执行,
		//所以当线程执行到t.falg=false时,会直接执行到show方法
		try{Thread.sleep(10);}catch(Exception e){}
		t.flag = false;
		t2.start();
		
	}
}

多线程-死锁

//定义锁的对象
class Dea implements Runnable
{
	//定义一个标记
	private boolean flag;
	
	//初始化
	Dea(boolean flag)
	{
		this.flag = flag;
	}
	
	//覆盖Run方法
	public void run()
	{
		//创建同步锁,并互相嵌套
		if(flag)
		{
			synchronized(Lock.lockA)
			{
				System.out.println("Lock.lockA");
				synchronized(Lock.lockB)
				{
					System.out.println("Lock.lockB");
				}
			}
		}
		else
		{
			synchronized(Lock.lockB)
			{
				System.out.println("Lock.lockB");
				synchronized(Lock.lockA)
				{
					System.out.println("Lock.lockA");
				}
			}
		}
	}
}

//创建锁对象
//加static:原因是可以直接用类名.lock访问
class Lock
{
	static Object lockA = new Object();
	static Object lockB = new Object();
}
public class _09死锁创建 {
	
	public static void main(String[]args)
	{
		Thread t1 = new Thread(new Dea(true));
		Thread t2 = new Thread(new Dea(false));
		
		t1.start();
		t2.start();
	}

}

线程间通信 等待唤醒机制



/*

 * 线程间通讯:输入,,,输出

 * 其实就是多个线程在操作同一资源。

 * 但是操作的动作不同。

 *

 * 注意:同步共享数据和绑定同一对象

 *

 *

 * 《多线程的-----唤醒机制》-----

 * ------------------------------------

 * wait();

 * notify();

 * notifyAll();

 * 都使用在同步中,因为要对持有监视器(锁)的线程操作

 * 所以要使用在同步中,因为只有同步才具有锁

 *

 * 为什么这些操作线程的方法要定义在Object类中呢?

 * -----------------------

 * 因为只有这些方法在操作同步线程时,都必须标识它们所操作线程持有的锁。

 * 只有同一锁上的被等待线程,可以被同一个锁上的notify()唤醒;

 * 不可以对不同锁中的线程唤醒。

 *

 * --也就是说等待和唤醒必须是同一个锁。

 * --而锁可以是任意对象,可以被任意对象调用的方法定义在Object

 */

class Res
{
	String name;
	String sex;
	boolean flag = false;//标记
}
class Input implements Runnable
{
	private Res r;
	Input(Res r)
	{
		this.r=r;
	}
	public void run()
	{
		int x = 0;
		while(true)
		{
			synchronized(r)
			{
				//4.循环再判断上面的条件。就为真了,然后就等着了wait();
				if(r.flag)//如果条件为真,就等一下
				{
					try{r.wait();}catch(Exception e){}
				}
				//1.因为第一次标记不为真。所以就打印一次NIKE
				if(x==0)
				{
					r.name="NIKE";
					r.sex="MAN";
				}else
				{
					r.name="范冰冰";
					r.sex="女女女女女女女";
				}
				x = (x + 1)% 2;
				//2.打印完后,将标记改为true。然后唤醒输出打印Out
				r.flag = true;
				r.notify();
				//3.到这里,
			}
		}
	}
}
class Out implements Runnable
{
	private Res r; //操作同一资源,对其进行初始化(有了车就得先有煤),直接将资源对象传递进来
	Out(Res r)
	{
		this.r=r;
	}
	public void run()
	{
		while(true)
		{
			synchronized(r)
			{
				//7.再循环判断,到这里,就为假了,等待wait();
				if(!r.flag)
				{
					try{r.wait();}catch(Exception e){}
				}
				//5.为真,就打印一次
				System.out.println("姓名:"+r.name+"....性别:"+r.sex);
				//6.将标记改为假。唤醒
				r.flag = false;
				r.notify();
			}
		}
	}
}
public class _17多线程_唤醒机制 {
	
	public static void main(String[]args)
	{
		Res r = new Res();
		
		//两辆大卡车
		Input in = new Input(r);//装煤
		Out o = new Out(r);//卸煤
		
		//创建两个线程
		Thread t1 = new Thread(in);
		Thread t2 = new Thread(o);
		t1.start();
		t2.start();
	}

}

4个线程,生产与消费


/*

 * 对于多个生产者和消费者

 * 为什么要定义while判断标记

 * 原因:被唤醒的线程,再一次判断标记

 *

 * ------------------

 * 为什么定义notifyAll

 * 因为需要唤醒对方线程

 * 因为只用notify,容易出现只唤醒本方线程的情况。导致程序中所有的线程都等待。

 */

class Resours
{
	private String name;
	private int count=1;
	boolean flag = false;
	public synchronized void set(String name)
	{
		while(flag)//循环判断
		{
			try{wait();}catch(Exception e){}//t1(获取资格) t2(放弃资格)
		}
		this.name = name+"..."+count++;
		System.out.println(Thread.currentThread().getName()+"...生产者"+this.name);
		flag = true;
		this.notifyAll();//唤醒全部线程
	}
	public synchronized void out()
	{
		while(!flag)//循环判断
		{
			try{wait();}catch(Exception e){}//t3(放弃资格) t4(放弃资格)
		}
		System.out.println(Thread.currentThread().getName()+"......执行者"+this.name);
		flag = false;
		this.notifyAll();//唤醒全部线程
	}
}
class Producer implements Runnable//生产
{
	private Resours res;
	Producer(Resours res)
	{
		this.res = res;
	}
	public void run()
	{
		while(true)
		{
			res.set("商品");
		}
	}
}
class Consumer implements Runnable//销售
{
	private Resours res;
	Consumer(Resours res)
	{
		this.res = res;
	}
	public void run()
	{
		while(true)
		{
			res.out();
		}
	}
}
public class _17多线程_生产消费者 {
	
	public static void main(String[]args)
	{
		Resours res = new Resours();
		
		Producer p = new Producer(res);
		Consumer c = new Consumer(res);
		
		Thread t1 = new Thread(p);
		Thread t2 = new Thread(p);
		Thread t3 = new Thread(c);
		Thread t4 = new Thread(c);
		t1.start();
		t2.start();
		t3.start();
		t4.start();
	}

}

JDK1.5中的升级锁Lock

/*

 * Lock:锁

 * ReentrantLock: 重复锁定

 * Lock lock = new ReentrantLock();//Lock类下的子类对象,创建可重复使用的锁

 *   |--lock:锁定...相当于synchronized

 *   |--unlock:释放锁

 * condition:条件、身份

 *   |--await:等待

 *   |--signal:发送信号,唤醒一个等待线程

 *   |--signalAll:唤醒所有等待线程

 *

 * InterruptedException:中断异常

 *

 * ---------------------------------------

 * JDK1.5中提供了升级版多线程解决方案。

 * 将同步Synchronized替换成显示Lock操作。

 * Object中的wait\notify()\notifyAll,替换成了Condition对象

 * 该对可以Lock锁,进行获取,同时可以创建多个锁对象

 *---------------------------------------------------------

 * 在该实例中,实现了本方只唤醒对方的操作。

 *

 * 记住:(问)JDK1.5中生产消费者,提供了什么替换机制呢?

 * (答)JDK1.5版本提供了显示锁机制,以及显示的锁对象的等待、唤醒、操作机制。

 *       同时,它把锁唤醒机制进行了封装,可以使一个锁对应多个对象。可以指定唤醒的对象。

 */

class Ress
{
	private String name;
	private String sex;
	boolean flag = false;
	
	//Lock类下的子类对象,创建可重复使用的锁
	private Lock lock = new ReentrantLock();
	//返回绑定到此Lock实例的新Condition实例    其实就是可以建立多个身份的锁
	Condition condition_pro = lock.newCondition();//生产锁对象
	Condition condition_con = lock.newCondition();//消费锁对象
	
	public void set(String name,String sex)throws InterruptedException
	{
		lock.lock();
		try
		{
			while(flag)
			{
				condition_pro.await();//生产锁等待
			}
			this.name = name;
			this.sex = sex;
			flag = true;
			condition_con.signal();//唤醒消费
		}
		finally
		{
			lock.unlock();//释放锁的动作,一定要执行。
		}
	}
	public void out()throws InterruptedException
	{
		lock.lock();
		try
		{
			while(!flag)
			{
				condition_con.await();//消费锁等待
			}
			System.out.println(this.name+".."+this.sex);
			flag = false;
			condition_pro.signal();//唤醒生产
		}
		finally
		{
			lock.unlock();
		}
	}
}
class Inputt implements Runnable
{
	private Ress r;
	Inputt(Ress r)
	{
		this.r = r;
	}
	public void run()
	{
		int x = 0;

		while(true)
		{
			try
			{
				if(x==0)
				{
					r.set("MIKE", "Man");
				}
				else
				{
					r.set("麦克", "女");
				}
				x=(x+1)%2;
			}
			catch(InterruptedException e)
			{}
		}
	}
}
class Outt implements Runnable
{
	private Ress r;
	Outt(Ress r)
	{
		this.r = r;
	}
	public void run()
	{
		try
		{
			while(true)
			{
				r.out();
			}
		}
		catch(InterruptedException e)
		{}
	}
}
public class _19多线程_升级锁 {
	
	public static void main(String[]args)
	{
		Resours res = new Resours();
		
		Producer pro = new Producer(res);
		Consumer con = new Consumer(res);
		
		Thread t1 = new Thread(pro);
		Thread t2 = new Thread(pro);
		Thread t3 = new Thread(con);
		Thread t4 = new Thread(con);
		t1.start();
		t2.start();
		t3.start();
		t4.start();
	}

}



---------------------- 黑马程序员 Android培训、期待与您交流! ----------------------

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值