为什么在synchronize()的括号里面加一个字符串就能让程序串行了?那两个对象是怎么就和这个字符串连在一起的啊?
老师我有一点不明白,那个for不是循环一次吗?
为什么可以多次打印?
class ManTou{ private int id; public ManTou(int id) { this.id = id; } public int getId() { return id; } public void setId(int id) { this.id = id; } } class Buffer{ private ManTou[] container=new ManTou[10];//存储馒头的容器 private int index=0; //下面定义一个存储馒头的方法 public synchronized void push(ManTou manTou){ //为了防止馒头存满了额外添加一个方法来判断 while (this.index==this.container.length){//使用while循环是为了增加代码的可判断性 //下面实现了一个方法wait等待 try { this.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } //下面添加一个方法用来唤醒 this.notify(); this.container[this.index]=manTou;//向容器中存放馒头 index++;//上一个位置存放过以后换到下一个位置存放 } //下面定义一个方法来取馒头 public synchronized ManTou qv(){ while (this.index==0){ try { this.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } //因为上面加过了这里减一次取相应位置的馒头 this.notify(); this.index--; return this.container[this.index]; } } //下面创建一个生产线程 class Shengchan extends Thread{ private Buffer b; public Shengchan(Buffer b){ this.b=b; } @Override public void run() { for(int i=0;i<10;i++){ ManTou m=new ManTou(i); this.b.push(m); System.out.println("生产了馒头编号"+m.getId()); } } } //下面创建一个消费馒头的线程 class XiaoFei extends Thread{ private Buffer b; public XiaoFei(Buffer b){ this.b=b; } @Override public void run() { for(int i=0;i<10;i++){ ManTou qv = this.b.qv(); System.out.println("消费馒头"+qv.getId()); } } } public class TestManTou { public static void main(String[] args) { Buffer b=new Buffer(); new Shengchan(b).start(); new XiaoFei(b).start(); } }
怎么没有生产就开始消费了,感觉代码也没有什么问题
class A implements Runnable{ private Thread b; public A(Thread b){ this.b = b; } @Override public void run() { for(int i=0;i<10;i++){ System.out.println(Thread.currentThread().g etName()+" A "+i); if(i == 5){ try { this.b.join(); } catch (InterruptedException e) { e.printStackTrace(); } } try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } } } class B implements Runnable{ @Override public void run() { for(int i=0;i<20;i++){ System.out.println(Thread.currentThread().g etName()+" B "+i); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } } } public class TestJoinThread { public static void main(String[] args) { Thread t1 = new Thread(new B()); Thread t = new Thread(new A(t1)); t.start(); t1.start(); for(int i=0;i<10;i++){ System.out.println(Thread.currentThread().g etName()+" "+i); if(i ==2){ try { t.join(); } catch (InterruptedException e) { e.printStackTrace(); } } try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } } }
上面是课程代码
问题:为什么Class b 实现了Runnable接口后,Class A 中的私有属性b的类型要设置为Thread,而不是B?
i==0时不是会让步吗?为什么最后还会出现i==0的情况?
为什么没有让步?
为什么当前线程的名字不是Test Thread 2?
老师为什么这个地方存到数组9的位置之后拿出的第一个是0的位置
这个文档在哪里能查看?
老师请问一下,这两个实例的名称为什么可以一样啊
还是没懂218行this和this.manager的区别
怎么回事?
老师,您好。我这个代码怎么不能执行,找不到run
package com.itbaizhan; import com.sun.org.apache.xpath.internal.operations.String; import java.io.FileInputStream; import java.io.IOException; public class Test2 { public static void main(String[] args) { FileInputStream fis = null; try{ fis = new FileInputStream("c:/a.txt"); StringBuilder sb = new StringBuilder(); int temp = 0; while ((temp = fis.read()) !=-1){ sb.append((char) temp); } System.out.println(sb); }catch (Exception e){ e.printStackTrace(); }finally { try { if(fis !=null) { fis.close(); } } catch (IOException e) { e.printStackTrace(); } } } }
非常抱歉给您带来不好的体验!为了更深入的了解您的学习情况以及遇到的问题,您可以直接拨打投诉热线:
我们将在第一时间处理好您的问题!
关于
课程分类
百战程序员微信公众号
百战程序员微信小程序
©2014-2025百战汇智(北京)科技有限公司 All Rights Reserved 北京亦庄经济开发区科创十四街 赛蒂国际工业园网站维护:百战汇智(北京)科技有限公司 京公网安备 11011402011233号 京ICP备18060230号-3 营业执照 经营许可证:京B2-20212637