用JAVA语言通过实现Runnable接口方式创建并启动线程,线程中实现在控制台上打印“hello thread”的功能。 步骤如下: 1.创建一个类实现Runnable接口 2.重写run()方法,在方法内实现打印“hello thread”的功能 3.创建Thread类对象,将实现Runnable接口的类的对象做为参数,传递到Thread类的Thread(Runnable runable)构造方法中 4.通过Thread类对象调用start()方法开启线程
时间: 2024-02-06 07:10:25 浏览: 125
下面是示例代码:
```
public class HelloThread implements Runnable {
@Override
public void run() {
System.out.println("Hello thread");
}
public static void main(String[] args) {
HelloThread helloThread = new HelloThread();
Thread thread = new Thread(helloThread);
thread.start();
}
}
```
这个程序创建了一个实现了Runnable接口的类HelloThread,并重写了run()方法,在其中打印了"Hello thread"。在main()方法中,创建了一个Thread类的对象,并将HelloThread类的对象作为参数传递到Thread类的构造方法中。最后通过调用start()方法开启线程,执行run()方法中的代码。
相关问题
通过实现Runnable接口方式创建并启动线程,线程中实现在控制台上打印“hello thread”的功能。 步骤如下: 1.创建一个类实现Runnable接口 2.重写run()方法,在方法内实现打印“hello thread”的功能 3.创建Thread类对象,将实现Runnable接口的类的对象做为参数,传递到Thread类的Thread(Runnable runable)构造方法中 4.通过Thread类对象调用start()方法开启线程
代码如下:
```java
public class MyRunnable implements Runnable {
@Override
public void run() {
System.out.println("hello thread");
}
}
public class Main {
public static void main(String[] args) {
MyRunnable myRunnable = new MyRunnable();
Thread thread = new Thread(myRunnable);
thread.start();
}
}
```
运行结果为:
```
hello thread
```
(1) 将课程中的tcpthread.SimpleTCPClient修改为多线程方式,模拟多个客户端同时向服务的发起服务情况; (2) 客户端main方法中,根据String[] msgs = {“Hello ”," Whut", "Info ", "2021 ","Java "}; 字符串长度创建5个线程,第i个线程完成(a)连接服务器端;(b)主动向服务器发送第i个字符串;(c)接收服务器的返回,并输出到控制台 (3) 服务器仍采用多线程方式。main线程用于等待客户端请求,接收到客户端请求后,创建一个新线程处理该客户端请求; (4) 服务器线程完成的工作主要有:(a)接收客户端线程发送的消息msg,并输出到控制台;(b)将按 “Echo from server: ” + msg 方式拼接后的数据发送给客户端;(c)关闭端口
本人将根据您的需求提供以下代码,分别为客户端和服务器端。请注意代码中的注释和说明。
客户端代码:
```java
import java.io.*;
import java.net.*;
public class MultiThreadTCPClient implements Runnable {
private String msg;
public MultiThreadTCPClient(String msg) {
this.msg = msg;
}
@Override
public void run() {
Socket socket;
try {
// 连接服务器
socket = new Socket("localhost", 8888);
// 发送消息
OutputStream os = socket.getOutputStream();
os.write(msg.getBytes());
// 接收服务器返回的消息
InputStream is = socket.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String response = br.readLine();
System.out.println("Response from server: " + response);
// 关闭连接
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
String[] msgs = {"Hello ", " Whut", "Info ", "2021 ", "Java "};
// 创建5个线程,分别发送5个消息
for (int i = 0; i < msgs.length; i++) {
Thread thread = new Thread(new MultiThreadTCPClient(msgs[i]));
thread.start();
}
}
}
```
服务器端代码:
```java
import java.io.*;
import java.net.*;
public class MultiThreadTCPServer {
public static void main(String[] args) {
ServerSocket serverSocket;
try {
// 创建服务器端Socket,绑定端口
serverSocket = new ServerSocket(8888);
System.out.println("Server started. Listening on port " + serverSocket.getLocalPort() + "...");
// 循环等待客户端连接请求
while (true) {
Socket socket = serverSocket.accept();
System.out.println("Client connected. Remote address: " + socket.getRemoteSocketAddress());
// 创建新线程处理该客户端请求
Thread thread = new Thread(new ClientHandler(socket));
thread.start();
}
} catch (IOException e) {
e.printStackTrace();
}
}
private static class ClientHandler implements Runnable {
private Socket socket;
public ClientHandler(Socket socket) {
this.socket = socket;
}
@Override
public void run() {
try {
// 接收客户端发送的消息
InputStream is = socket.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String msg = br.readLine();
System.out.println("Message from client: " + msg);
// 发送消息给客户端
OutputStream os = socket.getOutputStream();
os.write(("Echo from server: " + msg).getBytes());
// 关闭连接
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
```
阅读全文