1.分别运用Runnable和Callable接口实现线程 2.分别运用同步代码段、同步方法和同步锁的方法实现线程同步 3.实验UDP协议实现用户信息的发送和接受功能 4.运用TCP协议实现向服务器上传文件
时间: 2024-01-09 20:04:30 浏览: 129
1. 运用Runnable和Callable接口实现线程
Runnable接口和Callable接口都可以用来实现线程,但是Callable接口可以返回执行结果,而Runnable接口不能。下面分别给出两种方式的示例代码:
(1)使用Runnable接口实现线程
```java
public class MyRunnable implements Runnable {
@Override
public void run() {
// 线程执行的代码
System.out.println("线程开始执行");
}
}
// 在主函数中创建线程并启动
public static void main(String[] args) {
Thread thread = new Thread(new MyRunnable());
thread.start();
}
```
(2)使用Callable接口实现线程
```java
public class MyCallable implements Callable<String> {
@Override
public String call() throws Exception {
// 线程执行的代码
return "线程执行完毕";
}
}
// 在主函数中创建线程并启动
public static void main(String[] args) throws ExecutionException, InterruptedException {
ExecutorService executorService = Executors.newSingleThreadExecutor();
Future<String> future = executorService.submit(new MyCallable());
String result = future.get();
System.out.println(result);
executorService.shutdown();
}
```
2. 分别运用同步代码段、同步方法和同步锁的方法实现线程同步
Java中的线程同步可以通过synchronized关键字来实现,有三种方式:同步代码段、同步方法和同步锁。下面分别介绍这三种方式的实现方法。
(1)同步代码段
```java
public class MyRunnable implements Runnable {
private int count = 0;
@Override
public void run() {
synchronized (this) { // 使用this作为锁对象
for (int i = 0; i < 5; i++) {
count++; // 非原子操作
System.out.println(Thread.currentThread().getName() + " count=" + count);
}
}
}
}
```
(2)同步方法
```java
public class MyRunnable implements Runnable {
private int count = 0;
@Override
public synchronized void run() { // 使用synchronized修饰方法
for (int i = 0; i < 5; i++) {
count++; // 非原子操作
System.out.println(Thread.currentThread().getName() + " count=" + count);
}
}
}
```
(3)同步锁
```java
public class MyRunnable implements Runnable {
private int count = 0;
private final Object lock = new Object(); // 定义一个锁对象
@Override
public void run() {
synchronized (lock) { // 使用lock作为锁对象
for (int i = 0; i < 5; i++) {
count++; // 非原子操作
System.out.println(Thread.currentThread().getName() + " count=" + count);
}
}
}
}
```
3. 实验UDP协议实现用户信息的发送和接受功能
UDP协议是一种无连接的协议,它不保证数据传输的可靠性、顺序性和唯一性。下面给出发送和接收用户信息的示例代码:
(1)发送用户信息
```java
public class UDPSender {
public static void main(String[] args) throws IOException {
DatagramSocket socket = new DatagramSocket();
String message = "Hello, World!";
byte[] data = message.getBytes();
InetAddress address = InetAddress.getByName("127.0.0.1");
int port = 8888;
DatagramPacket packet = new DatagramPacket(data, data.length, address, port);
socket.send(packet);
socket.close();
}
}
```
(2)接收用户信息
```java
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
public class TCPClient {
public static void main(String[] args) throws IOException {
Socket socket = new Socket("localhost", 8888);
OutputStream outputStream = socket.getOutputStream();
FileInputStream fileInputStream = new FileInputStream("D:\\test.txt");
byte[] buffer = new byte[1024];
int len;
while ((len = fileInputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, len);
}
fileInputStream.close();
outputStream.close();
socket.close();
}
}
```
```java
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("D:\\test_copy.txt");
byte[] buffer = new byte[1024];
int len;
while ((len = inputStream.read(buffer)) != -1) {
fileOutputStream.write(buffer, 0, len);
}
fileOutputStream.close();
inputStream.close();
socket.close();
serverSocket.close();
}
}
```
阅读全文