老师你好
请问数组是放在类中声明,还是放在main方法中声明使用?
如果数组是本身是引用类型,那么像“ int [ ][ ] a ; ”中的int是什么意思?
/** * 账户类 */ class Account1{ // 账户 private String account1; // 账户余额 private double balance; public Account1() { } public Account1(String account1, double balance) { this.account1 = account1; this.balance = balance; } public String getAccount1() { return account1; } public void setAccount1(String account1) { this.account1 = account1; } public double getBalance() { return balance; } public void setBalance(double balance) { this.balance = balance; } } /** * 取款线程 */ class DrawThread1 extends Thread{ // 账户对象 private Account1 account1; // 取款金额 private double drawMoney; public DrawThread1(String name,Account1 account1,double drawMoney){ super(name); this.account1 = account1; this.drawMoney = drawMoney; } /** * 取款线程的线程体 */ @Override public void run() { // 使用synchronized块实现线程同步 synchronized (this.account1) { // 当前账户对象作为线程锁 if(this.account1.getBalance() >= this.drawMoney){ System.out.println("取款成功,吐出钞票:"+this.drawMoney); try { Thread.sleep(1000); // 线程休眠1s } catch (InterruptedException e) { e.printStackTrace(); } // 更新账户余额 this.account1.setBalance(this.account1.getBalance() - this.drawMoney); System.out.println("账户余额:"+this.account1.getBalance()); } else { System.out.println("取款失败,账户余额不足"); } } } } /** * 取款 */ public class DrawMoneyThread2 { public static void main(String[] args) { // 实例化账户对象 Account1 account1 = new Account1("1234",1000); // 启动线程 new DrawThread1("老公",account1,800).start(); new DrawThread1("老婆",account1,800).start(); } }
为什么线程名没有设置成功?
老师,这个SqlSessionFactoryBean又不是SqlSessionFactory的子类,怎么就能直接注入,想不通为什么?
老师,这个扩容的话,是随机的吗,容量是直接添加到数组后侧吗
import math x1 = int(input("请输入点1的x轴坐标:")) y1 = int(input("请输入点1的y轴坐标:")) x2 = int(input("请输入点2的x轴坐标:")) y2 = int(input("请输入点2的y轴坐标:")) dian1 = (x1,y1) dian2 = (x2,y2) while True: x3 = int(input("请输入点3的x轴坐标:")) y3 = int(input("请输入点3的y轴坐标:")) dian3 = (x3,y3) if (x3-x2)/(y3-y2)==(x3-x1)/(y3-y1): print("请重新输入:") else: break l1 = math.sqrt((x1-x2)**2+(y1-y2)**2) l2 = math.sqrt((x1-x3)**2+(y1-y3)**2) l3 = math.sqrt((x3-x2)**2+(y3-y2)**2) c = l1+l2+l3 area = math.sqrt(c/2*(c/2-l1)(c/2-l2)*(c/2-l3)) print("三角形的周长是"+str(c)+":"+"三角形的面积是"+str(area))
作业不会做,我这个报错是因为,浮点数不能参与计算吗?我要怎么解决这个问题
老师我想问一下:工作当中是继承Thread类用的多一点,还是实现Runnable接口用的多一点。
老师,请问这些容器存元素的时候,除了一个一个put/add进去,还可以怎么操作?
老师,那两个java文件有什么关联?会不会同时跑起来的?
老师,这里在保存到数据库之前,不应该还有一步么,得到orderedict,serializer.validated_data,之后在save么
老师,你说xml文件在有约束的情况下,不能随意更改属性。但要是万一在工作中需要对有约束的xml文件,进行获取结点的信息,应该怎么做?假如想使用css选择器解析获取其中的某个元素节点的文本,那就要添加id或class属性,但是又不能随意添加,该怎么办?求解
老师,我一登录就报这个异常是怎么回事呢,
老师麻烦帮我看一下 无法更新状态其他操作都没有问题
ajaxdemo.zip
i = 0
sum = 0
while i<=100:
i += 1
if i%2==0:
continue
if i%2==1:
sum = sum + i
print(sum)
这个奇数求和为什么算出来会多出100呢?
package G_Multithreading.E_Synchronized; //面包类 class Bread{ private int id; public Bread(int id){ this.id = id; } } //缓冲区 class SynBuffer{ //存放面包的盒子 private Bread[] breads = new Bread[10]; //存放面包盒子的索引 private int index; //放面包 public synchronized void push(Bread bread){ //判断盒子是否存满 while (breads.length == this.index + 1){ try { /* 语法:wait(),该方法必须要在synchronized块中调用。 wait执行后,线程会将持有的对象锁释放,并进入阻塞状态, 其他需要该对象锁的线程就可以继续运行了。 */ this.wait(); } catch (InterruptedException e) { e.printStackTrace(); } /* 语法:该方法必须要在synchronized块中调用。 该方法会唤醒处于相同对象的,并且等待状态队列中的一个线程。 */ //提醒取面包 this.notify(); breads[this.index] = bread; this.index++; } } //取走面包 public synchronized Bread pop(){ while (breads.length == 0) { try { this.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } this.notify(); this.index--; return breads[this.index]; } } //生产面包线程 class Produce extends Thread{ SynBuffer synBuffer; public Produce(SynBuffer synBuffer){ this.synBuffer = synBuffer; } @Override public void run() { for (int i = 0; i < 10; i++){ Bread bread = new Bread(i); this.synBuffer.push(bread); System.out.println("生产面包" + i); } } } //消费者线程 class Customer extends Thread{ SynBuffer synBuffer; public Customer(SynBuffer synBuffer){ this.synBuffer = synBuffer; } @Override public void run() { for (int i = 0; i < 10; i++){ Bread bread = this.synBuffer.pop(); System.out.println("取走面包" + i); } } } public class SynchronizedBuffer { public static void main(String[] args) { SynBuffer synBuffer = new SynBuffer(); new Produce(synBuffer).start(); new Customer(synBuffer).start(); } }
老师,他到取出的是够就高速index是-1,哪里出错了?
非常抱歉给您带来不好的体验!为了更深入的了解您的学习情况以及遇到的问题,您可以直接拨打投诉热线:
我们将在第一时间处理好您的问题!
关于
课程分类
百战程序员微信公众号
百战程序员微信小程序
©2014-2025百战汇智(北京)科技有限公司 All Rights Reserved 北京亦庄经济开发区科创十四街 赛蒂国际工业园网站维护:百战汇智(北京)科技有限公司 京公网安备 11011402011233号 京ICP备18060230号-3 营业执照 经营许可证:京B2-20212637