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

黑马程序员_java_多线程

2014-01-29 11:33 281 查看

------- <a href="http://www.itheima.com" target="blank">android培训</a>、<a href="http://www.itheima.com" target="blank">java培训</a>、期待与您交流! ----------

多线程:

进程:一个正在执行的程序,

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

线程:进程中一个独立的控制单元。

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

定义方式:

第一种:定义类继承于Thread类,复写run方法;

第二种:实现Runnable接口。复写run()方法

实现接口 Runnable:

步骤:

1,定义类实现Runnable接口

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

将线程代码放在run方法中。

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

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

因为自定义run方法所属对象是Runnable接口的子类对象,所以要让线程去指定对象的run方法,就必须明确run方 法的所属对象

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

实现和继承的区别:

实现方式:避免的单继承的局限性。(建议使用实现Runnable接口方式)

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

实现Runnable:线程代码存放于接口子类分run方法中。

同步代码块:

synchroized(对象){//Object obj = new Objece();

同步代码

}

同步的前提:

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

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

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

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

同步函数:把synchronized 作为修饰符放在函数上。

静态函数:静态同步函数的的锁是该方法所在类的字节码文件对象:类名.class

死锁:

/*

死锁

*/

class Lack implements Runnable

{

Object lacka = new Object();

Object lackb = new Object();

private boolean flag;

public Lack(boolean flag)

{

this.flag= flag;

}

public void run()

{

if(flag)

{

while(true)

{

synchronized(lacka)

{

System.out.println("if lacka");

synchronized(lackb)

{

System.out.println("if lackb");

}

}

}

}

else

{

while(true)

{

synchronized(lackb)

{

System.out.println("else lackb");

synchronized(lacka)

{

System.out.println("else lacka");

}

}

}

}

}

}

class MyLack

{

public static void main(String[] args)

{

Thread t1 = new Thread(new Test(true));

Thread t2 = new Thread(new Test(true));

t1.start();

t2.start();

}

}

面试常考:懒汉式 单例设计模式

/*

懒汉式单例设计模式

*/

class Single

{

private static Single s = null;

private Single(){}

public static Single getInstance(){

if(s==null){

synchronized (Single.class){

if(s==null)

s = new Single();

}

} return s;

}

}

编程代码:售票。

class Ticket implements Runnable //extends Thread

{

private int tick =100;

public void run(){

if(tick >0){

while(true){

System.out.println(Thread.currentThread().getName()+tick--);

}

}

}

}

class TicketDemo

{

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);

//Ticket t1 = new Ticket();

//Ticket t2 = new Ticket();

//Ticket t3 = new Ticket();

//Ticket t4 = new Ticket();

t1.start();

t2.start();

t3.start();

t4.start();

}

}

/*

生产者 消费者。

*/

public class ProducerDemo

{

public static void main(String[] args)

{

Resource r = new Resource();

Consumer con = new Consumer(r);

Producer pro = new Producer(r);

Thread t1 = new Thread(con);

Thread t2 = new Thread(pro);

Thread t3 = new Thread(con);

Thread t4 = new Thread(pro);

t1.start();

t2.start();

t3.start();

t3.start();

}

}

class Resource

{

private String name;

private int count = 1;

private boolean flag = false;

public synchronized void set(String name)

{

while(flag)

{

try

{

this.wait();

}

catch (Exception e)

{

}

} this.name = name+"..."+count++;

System.out.println(Thread.currentThread().getName()+"...生产者..."+this.name);

flag =true;

this.notifyAll();

}

public synchronized void get()

{

while(!flag)

{

try

{

this.wait();

}

catch (Exception e)

{

}

}

System.out.println(Thread.currentThread().getName()+"---消费者---"+this.name);

flag = false;

this.notifyAll();

}

}

class Consumer implements Runnable

{

private Resource res;

public Consumer(Resource res)

{

this.res = res;

}

public void run()

{

while(true)

{

res.get();

}

}

}

class Producer implements Runnable

{

private Resource res;

public Producer(Resource res)

{

this.res = res;

}

public void run()

{

while(true)

{

res.set("商品");

}

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: