多线程

多线程实现两种方式 一种是

class 类名 extends Thread{

方法1;

方法2;

public void run(){

// other code…

}

属性1;

属性2;

 

}

 

另一种 

class 类名 implements Runnable{

方法1;

方法2;

public void run(){

// other code…

}

属性1;

属性2;

 

}

Thread 不能 实现资源共享 而 Runnable能够实现资源共享。  

 

 

 

 

线程的 同步   如买票系统:

 

第一种方法( 是锁住整个类 ):       

synchronized(同步对象){

 

 //需要同步的代码

 

}

 

class hello implements Runnable {

    public void run() {

        for(int i=0;i<10;++i){

            synchronized (this) {

                if(count>0){

                    try{

                        Thread.sleep(1000);

                    }catch(InterruptedException e){

                        e.printStackTrace();

                    }

                    System.out.println(count--);

                }

            }

        }

    }

 

    public static void main(String[] args) {

        hello he=new hello();

        Thread h1=new Thread(he);

        Thread h2=new Thread(he);

        Thread h3=new Thread(he);

        h1.start();

        h2.start();

        h3.start();

    }

    private int count=5;

}

 

 

第二种方法( 是锁住方法 ):

 

语法格式为synchronized 方法返回类型方法名(参数列表){

 

    // 其他代码

 

}

 

class hello implements Runnable {

    public void run() {

        for (int i = 0; i < 10; ++i) {

            sale();

        }

    }

 

    public synchronized void sale() {

        if (count > 0) {

            try {

                Thread.sleep(1000);

            } catch (InterruptedException e) {

                e.printStackTrace();

            }

            System.out.println(count--);

        }

    }

 

    public static void main(String[] args) {

        hello he = new hello();

        Thread h1 = new Thread(he);

        Thread h2 = new Thread(he);

        Thread h3 = new Thread(he);

        h1.start();

        h2.start();

        h3.start();

    }

 

    private int count = 5;

}

 

 

**********************************************************8

 

 

线程的礼让  yield()方法是将一个线程暂时交给其他线程执行

线程优先级设置  setPriority(8)    系统线程优先级为5

 

class hello implements Runnable {

    public void run() {

        for(int i=0;i<5;++i){

            System.out.println(Thread.currentThread().getName()+"运行"+i);

            if(i==3){

                System.out.println("线程的礼让");

                Thread.currentThread().yield();

            }

        }

    }

 

    public static void main(String[] args) {

        Thread h1=new Thread(new hello(),"A");

        Thread h2=new Thread(new hello(),"B");

h1.setPriority(8);                      //设置线程的优先等级

        h1.start();

        h2.start();

         

    }

}

*******************************************************

 

问:怎么避免 互锁

 

一般造成死锁必须同时满足如下4个条件:

  1,互斥条件:线程使用的资源必须至少有一个是不能共享的;

  2,请求与保持条件:至少有一个线程必须持有一个资源并且正在等待获取一个当前被其它线程持有的资源;

  3,非剥夺条件:分配资源不能从相应的线程中被强制剥夺;

  4,循环等待条件:第一个线程等待其它线程,后者又在等待第一个线程。

因为要产生死锁,这4个条件必须同时满足,所以要防止死锁的话,只需要破坏其中一个条件即可。

 

或者用 wait + notifyall()

while (buf.size()==0) {

 

  wait(); // called if the buffer is empty (try/catch removed for brevity)

 

  // X: this is where C1 tries to re-acquire the lock (see below)

 

  }

 

  xxx……

 

  notifyall(); // called if there are any getters or putters waiting

 

  }

 

**********************************************************************8

问:如果一个线程interrupt 是立即执行还是等待执行

 

public void interrupt()

这个方法用来中断线程。 

如果线程在调用sleep(2000) 方法过程中受阻,则其中断状态将被清除,它还将收到一个 

 

InterruptedException。 

 如果当前线程没有中断它自己,则该线程会调用 checkAccess 方法,这可能抛出 

 

SecurityException。 

checkAccess 方法:是用来判定当前运行的线程是否有权修改该线程。

 

转自好多 出处…………

 

********************************************* 多线程 防止 互锁 例子 *************************

package com.jameswxx.synctest;

public class Plate{

List<Object> eggs=new ArrayList<Object>();

public synchronized Object getEgg(){

if(eggs.size()==0){

try{

wait();

}catch(InterruptedException e){

}

}

Object egg=eggs.get(0);

eggs.clear();//清空盘子

notify();//唤醒阻塞队列的某线程到就绪队列

return egg;

}

public synchronized void putEgg(Object egg){

If(eggs.size()>0){

try{

wait();

}catch(InterruptedException e){

}

}

eggs.add(egg);//往盘子里放鸡蛋

notify();//唤醒阻塞队列的某线程到就绪队列

}

}

 

声明一个Plate对象为plate,被线程A和线程B共享,A专门放鸡蛋,B专门拿鸡蛋。假设

1 开始,A调用plate.putEgg方法,此时eggs.size()为0,因此顺利将鸡蛋放到盘子,还执行了notify()方法,唤醒锁的阻塞队

列的线程,此时阻塞队列还没有线程。

2 又有一个A线程对象调用plate.putEgg方法,此时eggs.size()不为0,调用wait()方法,自己进入了锁对象的阻塞队列。

3 此时,来了一个B线程对象,调用plate.getEgg方法,eggs.size()不为0,顺利的拿到了一个鸡蛋,还执行了notify()方法,

深入java虚拟机2.1 java线程安全总结

第 70 / 121 页

唤醒锁的阻塞队列的线程,此时阻塞队列有一个A线程对象,唤醒后,它进入到就绪队列,就绪队列也就它一个,因此马上得

到锁,开始往盘子里放鸡蛋,此时盘子是空的,因此放鸡蛋成功。

4 假设接着来了线程A,就重复2;假设来料线程B,就重复3。

整个过程都保证了放鸡蛋,拿鸡蛋,放鸡蛋,拿鸡蛋。

*********************************************************************************************************8

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值