Java使用synchronized实现多线程操作list<1>
2014-11-27 13:26
726 查看
Java的多线程实现方式有两种,一种是继承Thread类,一中是实现Runnable接口,这两种都要重写run方法,因为在run方法中存放的是要在多线程执行的代码,使用synchronized时要绑定一个对象,对于存在多个线程竞争的程序时就需要多个程序使用的是同一个锁资源,否则无法实现同步,具体的代码如下
下面以经典的生产者、消费者为例
生产者线程:
public class Producer implements Runnable {
private LinkedList<Student> mylist=new LinkedList<Student>();
private int MAX=100;
public Producer(){
}
public Producer(LinkedList<Student> list){
this.mylist=list;
}
public void run() {
// TODO Auto-generated method stub
while(true){
synchronized(mylist){
while(mylist.size()>MAX){
System.out.println(Thread.currentThread().getName()+ "仓库已满");
try {
mylist.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Student stu=new Student("张三", 20);
if(mylist.add(stu)){
System.out.println("生产了一个"+stu.getName());
mylist.notifyAll();
}
}
}
}
}
消费者线程:
public class Consumer implements Runnable{
private LinkedList<Student> mylist=new LinkedList<Student>();
public Consumer(){
}
public Consumer(LinkedList<Student> list){
this.mylist=list;
}
public void run() {
// TODO Auto-generated method stub
while(true){
synchronized (mylist) {
while(mylist.size()==0){
System.out.println(Thread.currentThread().getName()+ "仓库为空");
try {
mylist.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Student stu=mylist.removeLast();
System.out.println("消费了一个"+stu.getAge());
mylist.notifyAll();
}
}
}
}
实现多线程:
public class MoreThread {
private static LinkedList<Student> mylist=new LinkedList<Student>();
public static void main(String[] args) {
Thread t1=new Thread(new Producer(mylist));
Thread t2=new Thread(new Consumer(mylist));
Thread t3=new Thread(new Producer(mylist));
Thread t4=new Thread(new Consumer(mylist));
t1.setName("生产者1---");
t3.setName("生产者2---");
t2.setName("消费者1---");
t4.setName("消费者2---");
t1.start();
t2.start();
t3.start();
t4.start();
}
}
下面以经典的生产者、消费者为例
生产者线程:
public class Producer implements Runnable {
private LinkedList<Student> mylist=new LinkedList<Student>();
private int MAX=100;
public Producer(){
}
public Producer(LinkedList<Student> list){
this.mylist=list;
}
public void run() {
// TODO Auto-generated method stub
while(true){
synchronized(mylist){
while(mylist.size()>MAX){
System.out.println(Thread.currentThread().getName()+ "仓库已满");
try {
mylist.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Student stu=new Student("张三", 20);
if(mylist.add(stu)){
System.out.println("生产了一个"+stu.getName());
mylist.notifyAll();
}
}
}
}
}
消费者线程:
public class Consumer implements Runnable{
private LinkedList<Student> mylist=new LinkedList<Student>();
public Consumer(){
}
public Consumer(LinkedList<Student> list){
this.mylist=list;
}
public void run() {
// TODO Auto-generated method stub
while(true){
synchronized (mylist) {
while(mylist.size()==0){
System.out.println(Thread.currentThread().getName()+ "仓库为空");
try {
mylist.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Student stu=mylist.removeLast();
System.out.println("消费了一个"+stu.getAge());
mylist.notifyAll();
}
}
}
}
实现多线程:
public class MoreThread {
private static LinkedList<Student> mylist=new LinkedList<Student>();
public static void main(String[] args) {
Thread t1=new Thread(new Producer(mylist));
Thread t2=new Thread(new Consumer(mylist));
Thread t3=new Thread(new Producer(mylist));
Thread t4=new Thread(new Consumer(mylist));
t1.setName("生产者1---");
t3.setName("生产者2---");
t2.setName("消费者1---");
t4.setName("消费者2---");
t1.start();
t2.start();
t3.start();
t4.start();
}
}
相关文章推荐
- springmvc在业务控制方法中收集List<JavaBean>参数,实现批量处理
- <java><JTable>使用AbstractTableModel & JTable实现号码的分布图(初稿)
- 写一个MyList<T>的类,内部用T[]实现(不能使用系统的List<T>类),需要实现的接口如下:
- 使用java的List<map> 实现树状目录(后端部分)
- 在JavaScript中实现类似Java中List<Map>类型的数组
- 基于Java使用HashMap<String,List<String>>实现数据的缓存
- List<Map<String,Object>>使用Java代码遍历以获取String,Object的值
- 黑马程序员:Java基础总结----子接口 List<E>及其实现类
- 利用java反射机制实现List<Map<String, Object>>转化为List<JavaBean>
- 数组操作大全<Java实现>
- LDAP学习笔记<四>jldap实现Java对LDAP的基本操作
- <转>java编译问题:使用了未经检查或不安全的操作
- Java实现从IP和端口列表List<String>获取分离的IP和端口值
- List<T>采用delegate快速实现排序、查找等操作
- java 后台使用gson 将json字符串转换为List<Map<String,object>> 数组
- java 基于List<MultipartFile>实现多文件上传
- List<T>采用delegate快速实现排序、查找等操作
- List<T>采用delegate快速实现排序、查找等操作
- Java笔记3 多线程<1>线程概述、多线程的创建、多线程的安全问题、静态同步函数的锁、死锁
- 【Java】多线程<1>-概念及Thread类基本使用