会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 132886个问题
JAVA 全系列/第二阶段:JAVA 基础深化和提高/多线程技术(旧) 3886楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/手写服务器项目(旧) 3887楼

import java.util.ArrayList;

public class Tset {
	public static void main(String []args) {
		//创建集合对象,同时明确了集合中所存储的对象类型只能是Person 类型
		ArrayList<Person> al=new ArrayList<Person>();
		//创建Person类型对象添加到集合中
		Person p1=new Person("marry",20);
		Person p2=new Person("lili",29);
		Person p3=new Person("jack",18);
		al.add(p1);
		al.add(p2);
		al.add(p3);
		//遍历集合
		print(al);
		
		//创建集合用于存储student类型的对象
		ArrayList<Student> al2=new ArrayList<Student>();
		Student stu1=new Student("sean",20,"sxt1001");
		Student stu2=new Student("nico",19,"sxt1002");
		//添加到集合
		al2.add(stu1);
		al2.add(stu2);
		//遍历集合
		print(al2);
		System.out.println("************");
		//调用show()方法
		show(al);
		show(al2);
		ArrayList<Object> alobject=new ArrayList<Object>();
		Object ob1=new Object();
		Object ob2=new Object();
		alobject.add(ob1);
		alobject.add(ob2);
		show(alobject);
	}
	//person以及person子类可以使用
	public static void print(ArrayList<? extends Person> al) {//相当于ArrayList<Person> al=new ArrayList<Student>();不匹配
		for(Person p:al) {
			System.out.println(p);
		}
	}
	public static void show(ArrayList<? super Student> al) {
		for(Object obj:al) {
			System.out.println(obj);
		}
	}
}
public static void show(ArrayList<? super Student> al) {
		for(Object obj:al) {
			System.out.println(obj);
		}
	}

为什么不是下面这个呢?

for(Student stu:al) {
			System.out.println(stu);
		}


JAVA 全系列/第二阶段:JAVA 基础深化和提高/容器(旧) 3890楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/XML 技术(旧) 3893楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/XML 技术(旧) 3894楼

问题点:为什么消费者会乱序呢,不是同步类吗?



 商品类:

package cn.sxt.thread9;

public class Goods {
	private String name;
	private String brand;
	public Goods() {
		super();
		// TODO Auto-generated constructor stub
	}
	public Goods(String name, String brand) {
		super();
		this.name = name;
		this.brand = brand;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getBrand() {
		return brand;
	}
	public void setBrand(String brand) {
		this.brand = brand;
	}


}

 生产者:

package cn.sxt.thread9;

public class Producer implements Runnable {
	private Goods goods;
	private int i;
	public Producer(Goods goods){
		this.goods = goods;
	}

	@Override
	public void run() {
		//生产商品
		for(int i=0;i<10;i++){
			if(i%2!=0){				
				this.get("小馒头","旺仔");		
				
			}else{
				this.get("矿泉水","娃哈哈");			
		   }
		   }		
	}
	
	public synchronized void get(String name,String brand){
		goods.setName(name);			
			try {
				Thread.sleep(300);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			goods.setBrand(brand);
			
		
		System.out.println("生产者线程生产了:"+goods.getBrand()+"--------"+goods.getName());
		
	}



}

 消费者:

package cn.sxt.thread9;

public class Consumer implements Runnable {
	private Goods goods;
	public Consumer(Goods goods){
		this.goods = goods;
	}

	@Override
	public void run() {
		// 获取商品
		for(int i=0;i<10;i++){
			this.get();
	
		
	}
	}
	public synchronized void get(){
		try {
			Thread.sleep(300);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println("-----消费者线程取得了"+goods.getBrand()+"---------"+goods.getName());
	}



}

 测试类

package cn.sxt.thread9;

public class TestGoods {
	
	public static void main(String[] args){
		Goods g= new Goods();
		Thread t = new Thread(new Producer(g));
		Thread t1 = new Thread(new Consumer(g));
		t.start();
		t1.start();
		
	}

}

image.png

源代码

MyThread.rar


JAVA 全系列/第二阶段:JAVA 基础深化和提高/多线程技术(旧) 3897楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/手写服务器项目(旧) 3899楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

©2014-2025百战汇智(北京)科技有限公司 All Rights Reserved 北京亦庄经济开发区科创十四街 赛蒂国际工业园
网站维护:百战汇智(北京)科技有限公司
京公网安备 11011402011233号    京ICP备18060230号-3    营业执照    经营许可证:京B2-20212637