老师这是什么问题
请问通过哪个快捷键可以直接返回啊
package com.itbaizhan; class manTou{ private int id; public void setId(int id) { this.id = id; } public int getId() { return id; } } //定义缓冲区 不是线程类 class SyncStack{ //定义放馒头的容器 private manTou m[] = new manTou[10]; //索引 int i; //放馒头 public synchronized void push(manTou mantou){ //判断是否满了 while(i == m.length) { //用while多判断几次防止当前线程没有执行而忽略 try { this.wait(); //object类的方法,必须在syn调用,线程的对象锁解放,并且进入堵塞状态 } catch (InterruptedException e) { e.printStackTrace(); } } this.notify(); //唤醒等待序列中的一个线程 this.m[i] = mantou; i++; } //取馒头 public synchronized manTou pop() { //判断是否满了 while (i == 0) { //用while多判断几次防止当前线程没有执行而忽略 try { this.wait(); //object类的方法,必须在syn调用,线程的对象锁解放,并且进入堵塞状态 } catch (InterruptedException e) { e.printStackTrace(); } } this.notify(); //唤醒等待序列中的一个线程 i--; return m[i]; } } //定义生产者线程 class shengchan extends Thread{ private SyncStack s; public shengchan(SyncStack s) { this.s = s; } @Override public void run() { for(int i = 0; i<10; i++){ System.out.println("生产馒头" + i); manTou m = new manTou(); // 每次循环都创建一个新的馒头并放入stack里 s.push(m); } } } class xiaofei extends Thread{ private SyncStack s; public xiaofei(SyncStack s) { this.s = s; } @Override public void run() { for(int i = 0; i < 10; i++){ System.out.println("取馒头" + i); manTou m = s.pop(); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } } } public class ProducerThread { public static void main(String[] args) { //创建缓冲区 SyncStack s = new SyncStack(); //创建生产线程 shengchan produce = new shengchan(s); produce.start(); //创建消费线程 xiaofei consumer = new xiaofei(s); consumer.start(); } }
QQ图片20230207232704.png
这里两个线程锁的都是同一个对象,那按理说应该等生产线程执行完再执行消费啊,为什么还是并发执行的?
为什么还要声明return?
老师 ,麻烦帮我看一下为什么我的getName标红
它提示我: 无法从static上下文l用非static方法'getName(T)'
请问这一步是什么意思啊
这两者区别有什么不同吗?为啥一个会报错另一个不会
package actualPractice.Practices.TestIO; import java.io.FileInputStream; import java.io.IOException; /** * IO经典流方法 */ public class TestIO { public static void main(String[] args) { FileInputStream fis=null; try { fis=new FileInputStream("d:/a.txt"); StringBuilder sb = new StringBuilder(); int temp=0; //当temp=-1时,表示已经到了文件结尾,停止读取 while((temp=fis.read()) !=-1){ sb.append((char) temp); } System.out.println(sb); } catch (IOException e) { throw new RuntimeException(e); }finally{ //这种写法,保证了即使遇到异常情况,也会关闭对象 if(fis!=null){ try { fis.close(); } catch (IOException e) { throw new RuntimeException(e); } } } } }
老师,文件里everyday后面是感叹号!,请问运行后显示的为什么是这样的?怎么改过来?
java.io.BufferedReaderjava.io.InputStreamReaderjava.io.PrintWriterjava.net.ServerSocketjava.net.Socketjava.util.ScannerSend Thread { Socket (Socket socket) { .= socket} () { .sendMsg()} () { (Scanner scanner = Scanner(System.)PrintWriter pw = PrintWriter(..getOutputStream())) { () { String msg = scanner.nextLine()pw.println(msg)pw.flush()} } (Exception e) { e.printStackTrace()} } } Receive Thread{ Socket (Socket socket){ .=socket} () { .receiveMsg()} () { (BufferedReader br = BufferedReader(InputStreamReader(..getInputStream()))) { () { String msg = br.readLine()System..println(+ msg)} } (Exception e) { e.printStackTrace()} } } ChatSocketServer { (String[] args) { (ServerSocket serverSocket=ServerSocket()){ System..println()Socket socket=serverSocket.accept()System..println()Send(socket).start()Receive(socket).start()}(Exception e){ e.printStackTrace()} } }
老师,麻烦帮我看看我这个代码哪里出问题了
lambda的参数传入类型只能是int类型吗?
老师这里为什么还要强转换一下呢
老师这里for循环的第三个语句为啥什么都不写
老师我想问一下bos的write方法输出的是啥呀?
老师创建ManTou对象,为什么后面会有个i呀,那是做什么的,没有就报错
非常抱歉给您带来不好的体验!为了更深入的了解您的学习情况以及遇到的问题,您可以直接拨打投诉热线:
我们将在第一时间处理好您的问题!
关于
课程分类
百战程序员微信公众号
百战程序员微信小程序
©2014-2025百战汇智(北京)科技有限公司 All Rights Reserved 北京亦庄经济开发区科创十四街 赛蒂国际工业园网站维护:百战汇智(北京)科技有限公司 京公网安备 11011402011233号 京ICP备18060230号-3 营业执照 经营许可证:京B2-20212637