class Account implements Runnable{ private int money; //修改方法 @Override public void run() { // TODO Auto-generated method stub } 给上代码写一个访问方法
时间: 2024-03-26 16:42:08 浏览: 119
以下是一个简单的访问方法,可以获取 Account 对象中的 money 属性:
```
public synchronized int getMoney() {
return money;
}
```
这里使用 synchronized 关键字来保证线程安全,确保在多线程环境下获取 money 属性的值是正确的。
相关问题
优化代码运用线程知识package D18; public class D18_1 { public static void main(String[] args) { // TODO Auto-generated method stub } } class Account implements Runnable{ private int money; //修改方法 //存钱 public void deposit(int amount) { money +=amount; System.out.println("存钱成功,现有"+money); } @Override public void run() { // TODO Auto-generated method stub deposit(100); } public int getMoney() { return money; } public void setMoney(int money) { this.money = money; } }
以下是对代码的优化,使用线程来实现存钱和查询余额的功能:
```
public class D18_1 {
public static void main(String[] args) throws InterruptedException {
Account account = new Account();
Thread depositThread = new Thread(() -> {
for (int i = 0; i < 10; i++) {
account.deposit(100);
}
});
Thread checkThread = new Thread(() -> {
for (int i = 0; i < 10; i++) {
System.out.println("当前余额为:" + account.getMoney());
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
depositThread.start();
checkThread.start();
depositThread.join();
checkThread.join();
}
}
class Account {
private int money;
public synchronized void deposit(int amount) {
money += amount;
System.out.println("存钱成功,现有" + money);
}
public synchronized int getMoney() {
return money;
}
}
```
这里使用了 synchronized 关键字来保证线程安全,同时使用了 join() 方法来保证查询余额的线程在存钱线程执行完毕后才开始运行,避免了查询余额时余额数值不正确的问题。
优化下面代码package Zzz; import java.awt.TexturePaint; import java.io.File; import java.io.FilenameFilter; import java.io.IOException; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetAddress; import java.text.BreakIterator; import java.util.Scanner; import javax.imageio.metadata.IIOInvalidTreeException; import javax.imageio.plugins.jpeg.JPEGImageWriteParam; import javax.sound.sampled.Port; import org.omg.CORBA.INTERNAL; public class Test3{ public static void main(String[] args) throws IOException { // TODO Auto-generated method stub Scanner scanner=new Scanner(System.in); System.out.print("请输入对方的IP地址"); String ipAddress=scanner.nextLine(); InetAddress ip=InetAddress.getByName(ipAddress); System.out.print("请输入对方的端口号"); int toPort=scanner.nextInt(); System.out.print("请输入本地接受的端口号:"); int listenPort=scanner.nextInt(); new Thread(); new Thread(); } } class SendThread implements Runnable{ private int Port; private InetAddress ip; public SendThread(int Port,InetAddress ip) { this.ip=ip; this.Port=Port; // TODO Auto-generated constructor stub } @Override public void run() { // TODO Auto-generated method stub try { Scanner scanner=new Scanner(System.in); DatagramSocket socket=new DatagramSocket(); System.out.println("开始聊天"); DatagramPacket packet=new DatagramPacket(new byte[1024], 1024, ip, Port); while(true){ String zyk=scanner.nextLine(); packet.setData(zyk.getBytes()); socket.send(packet); if (zyk.equals("bye")) { break; } } System.out.println("退出聊天"); socket.close(); } catch (Exception e) { // TODO: handle exception } } } class RecieveThread implements Runnable{ private int Port; public RecieveThread(int Port){ this.Port=Port; } @Override public void run() { while(true){ try { DatagramSocket socket=n
首先,你的代码存在一些问题,例如创建线程时没有指定线程任务,变量名不规范等等。以下是我对你的代码进行的优化:
1. 对变量名进行规范化,使其更易读懂。
2. 需要创建两个线程,一个用于发送消息,一个用于接收消息。因此我们需要在主线程中创建这两个线程,并且需要为它们指定任务。
3. 在发送消息的线程中,我们需要不断地从控制台读取用户输入,然后将其发送出去。需要注意的是,我们需要在循环中不断更新 DatagramPacket 对象中的数据部分。
4. 在接收消息的线程中,我们需要不断地接收来自对方的消息,并在控制台上输出。同样需要注意的是,在循环中需要不断更新 DatagramPacket 对象中的数据部分。
下面是优化后的代码:
```
package Zzz;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.util.Scanner;
public class Test3 {
public static void main(String[] args) throws Exception {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入对方的IP地址:");
String ipAddress = scanner.nextLine();
InetAddress ip = InetAddress.getByName(ipAddress);
System.out.print("请输入对方的端口号:");
int toPort = scanner.nextInt();
System.out.print("请输入本地接受的端口号:");
int listenPort = scanner.nextInt();
DatagramSocket socket = new DatagramSocket(listenPort);
Thread sendThread = new Thread(new SendTask(socket, ip, toPort));
Thread receiveThread = new Thread(new ReceiveTask(socket));
sendThread.start();
receiveThread.start();
}
}
class SendTask implements Runnable {
private final DatagramSocket socket;
private final InetAddress ip;
private final int toPort;
public SendTask(DatagramSocket socket, InetAddress ip, int toPort) {
this.socket = socket;
this.ip = ip;
this.toPort = toPort;
}
@Override
public void run() {
try {
Scanner scanner = new Scanner(System.in);
System.out.println("开始聊天");
while (true) {
String message = scanner.nextLine();
DatagramPacket packet = new DatagramPacket(message.getBytes(), message.getBytes().length, ip, toPort);
socket.send(packet);
if (message.equals("bye")) {
break;
}
}
System.out.println("退出聊天");
} catch (Exception e) {
e.printStackTrace();
} finally {
socket.close();
}
}
}
class ReceiveTask implements Runnable {
private final DatagramSocket socket;
public ReceiveTask(DatagramSocket socket) {
this.socket = socket;
}
@Override
public void run() {
try {
while (true) {
byte[] buffer = new byte[1024];
DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
socket.receive(packet);
String message = new String(packet.getData(), 0, packet.getLength());
System.out.println("对方说: " + message);
if (message.equals("bye")) {
break;
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
socket.close();
}
}
}
```
希望这些改进能帮助你更好地理解和使用该代码!
阅读全文