线程同步与死锁经典代码

一、添加同步的方法

package org.figo.thread;
1、同步语句块

class thread01 implements Runnable
{
 private int tickets;
 public thread01(int tickets)
 {
  this.tickets = tickets;
 }
 public void run() {
  synchronized(this){//同步语句块,使得有线程访问时,其他线程要等待该线程结束才能访问
  while(this.tickets > 0)
  {
   try {
    Thread.sleep(300);
   } catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
   System.out.println("还有" + tickets-- + "张票");
  }
  }
 
}
public class synchronized01 {
 public static void main(String args[])
 {
  thread01 t = new thread01(10);
  Thread t1 = new Thread(t);
  Thread t2 = new Thread(t);
  t1.start();
  t2.start();
 }
}
2、增加同步方法

package org.figo.thread;
class thread02 implements Runnable
{
 private int tickets;
 public thread02(int tickets)
 {
  this.tickets = tickets;
 }
 public void run() {
  this.sale();
  }

 public synchronized void sale()//增加同步方法,也可以实现线程的同步
 {
  while(this.tickets > 0)
  {
   try {
    Thread.sleep(300);
   } catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
   System.out.println("还有" + tickets-- + "张票");
  }
 }
}
public class CopyOfsynchronized02 {
 public static void main(String args[])
 {
  thread02 t = new thread02(10);
  Thread t1 = new Thread(t);
  Thread t2 = new Thread(t);
  t1.start();
  t2.start();
 }
}

2、消费者和生产者例程

文件一:产品信息

package org.figo.thread.producecustomer;

public class info {
 private String book;
 private int num;
 private boolean flag = false;
 public synchronized void setBook(String book, int num)
 {
  if(flag == true)
  {
   try {
    super.wait();//如果生产的还未被消费,则等待其他进程消费
   } catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
  }
  try {
   Thread.sleep(300);
  } catch (InterruptedException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  this.book = book;
  this.num = num;
  flag = true;//表示生产了,可以消费了
  super.notify();//生产好后,唤醒等待消费的进程
  System.out.println("生产了" + this.book + this.num + "本");
 }

 public synchronized String get() {
  if(flag == false)
  {
   try {
    super.wait();//如果还未生产,则等待生产
   } catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
  }
  try {
   Thread.sleep(300);
  } catch (InterruptedException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
      flag = false;//表示消费了,可以生产了
      super.notify();//消费后,唤醒等待生产的进程
   return "消费了" + this.book + this.num + " 本";
   
  }
}

文件二:生产者代码

package org.figo.thread.producecustomer;

public class producer implements Runnable{
 private info info;
 public producer(info info) {
  this.info = info;
 }
 public void run()
 {
  for (int i = 0; i < 20; i++)
  {
   if(i % 2 == 1)
    this.info.setBook("java", 8);
   else
    this.info.setBook("c++", 9);
  }
 }
}

文件三:消费者代码

package org.figo.thread.producecustomer;

public class customer implements Runnable{
 private info info;
 public customer(info info)
 {
  this.info = info;
 }
 public void run()
 {
  for(int i = 0; i < 20; i++)
   System.out.println(this.info.get());
 }

}

文件四:主方法

package org.figo.thread.producecustomer;

public class test {
 public static void main(String args[])
 {
  info info = new info();
  //p1和p2使用的是同一个info类
  producer p1 = new producer(info);
  customer p2 = new customer(info);
  Thread t1 = new Thread(p1);
  Thread t2 = new Thread(p2);
  t1.start();
  t2.start();
  
 }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值