.分别运用Runnable和Callable接口实现线程 2.分别运用同步代码段、同步方法和同步锁的方法实现线程同步 3.实验UDP协议实现用户信息的发送和接受功能 4.运用TCP协议实现向服务器上传文件
时间: 2023-11-27 18:53:47 浏览: 97
1. 分别运用Runnable和Callable接口实现线程:
Runnable接口是Java中定义线程的标准方式,它只有一个run()方法,线程执行的代码就在这个方法里。Callable接口也是Java中定义线程的一种方式,它也只有一个call()方法,与run()方法不同的是,call()方法可以返回一个结果。
使用Runnable接口实现线程:
```
public class MyRunnable implements Runnable {
public void run() {
// 线程执行的代码
}
}
// 创建线程并启动
MyRunnable myRunnable = new MyRunnable();
Thread thread = new Thread(myRunnable);
thread.start();
```
使用Callable接口实现线程:
```
public class MyCallable implements Callable<Integer> {
public Integer call() throws Exception {
// 线程执行的代码
return 123;
}
}
// 创建线程并启动
MyCallable myCallable = new MyCallable();
FutureTask<Integer> futureTask = new FutureTask<>(myCallable);
Thread thread = new Thread(futureTask);
thread.start();
// 获取线程执行结果
int result = futureTask.get();
```
2. 分别运用同步代码段、同步方法和同步锁的方法实现线程同步:
同步代码段:
```
public class MyRunnable implements Runnable {
private int count = 0;
private Object lock = new Object();
public void run() {
synchronized (lock) {
for (int i = 0; i < 10000; i++) {
count++;
}
}
}
}
```
同步方法:
```
public class MyRunnable implements Runnable {
private int count = 0;
public synchronized void run() {
for (int i = 0; i < 10000; i++) {
count++;
}
}
}
```
同步锁:
```
public class MyRunnable implements Runnable {
private int count = 0;
private Lock lock = new ReentrantLock();
public void run() {
lock.lock();
try {
for (int i = 0; i < 10000; i++) {
count++;
}
} finally {
lock.unlock();
}
}
}
```
3. 实验UDP协议实现用户信息的发送和接受功能:
UDP是一种无连接的传输协议,它不需要建立连接就可以发送数据包。UDP协议使用的是面向无连接的通信模式,它不保证数据传输的可靠性。在Java中,使用DatagramSocket和DatagramPacket类来实现UDP协议的数据发送和接收。
发送端代码:
```
public class UDPSender {
public static void main(String[] args) throws IOException {
DatagramSocket socket = new DatagramSocket();
String message = "Hello, UDP!";
byte[] data = message.getBytes();
InetAddress address = InetAddress.getByName("127.0.0.1");
DatagramPacket packet = new DatagramPacket(data, data.length, address, 8888);
socket.send(packet);
socket.close();
}
}
```
接收端代码:
```
public class UDPReceiver {
public static void main(String[] args) throws IOException {
DatagramSocket socket = new DatagramSocket(8888);
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("Received message: " + message);
socket.close();
}
}
```
4. 运用TCP协议实现向服务器上传文件:
TCP是一种可靠的面向连接的传输协议,它使用三次握手建立连接,保证数据的可靠传输。在Java中,使用Socket和ServerSocket类来实现TCP协议的数据传输。
客户端代码:
```
public class TCPClient {
public static void main(String[] args) throws IOException {
Socket socket = new Socket("127.0.0.1", 8888);
OutputStream outputStream = socket.getOutputStream();
FileInputStream fileInputStream = new FileInputStream("test.txt");
byte[] buffer = new byte[1024];
int len = 0;
while ((len = fileInputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, len);
}
fileInputStream.close();
socket.shutdownOutput();
InputStream inputStream = socket.getInputStream();
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
while ((len = inputStream.read(buffer)) != -1) {
byteArrayOutputStream.write(buffer, 0, len);
}
String result = new String(byteArrayOutputStream.toByteArray());
System.out.println(result);
byteArrayOutputStream.close();
inputStream.close();
outputStream.close();
socket.close();
}
}
```
服务端代码:
```
public class TCPServer {
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = new ServerSocket(8888);
Socket socket = serverSocket.accept();
InputStream inputStream = socket.getInputStream();
FileOutputStream fileOutputStream = new FileOutputStream("test_copy.txt");
byte[] buffer = new byte[1024];
int len = 0;
while ((len = inputStream.read(buffer)) != -1) {
fileOutputStream.write(buffer, 0, len);
}
fileOutputStream.close();
OutputStream outputStream = socket.getOutputStream();
outputStream.write("Upload success".getBytes());
outputStream.close();
inputStream.close();
socket.close();
serverSocket.close();
}
}
```
阅读全文