Java – 如何同时使用Executor Service运行两个静态和void函数

huangapple go评论115阅读模式
英文:

Java - How to simultaneously run two static and void functions with Executor Service

问题

我正在尝试让我的程序能够同时准备发送用户输入的消息并监听传入的消息。

我尝试使用Executor Service,但它总是出现一个错误,说这些函数不能是静态或void。

因此,我尝试将我需要同时运行的两个函数(这两个函数不是静态的并且返回一个我不会使用的字符串)设置为非静态,并且我认为这是因为我的函数使用了之前声明的静态类变量而导致的错误。

这是我得到的错误信息:

EchoClient.java:66: 错误: <匿名EchoClient$1>不是抽象的
并且没有覆盖抽象方法call() in Callable
callables.add(new Callable() {

这是我简化并且没有Executor Service的代码:

class EchoClient
{	
    public static DatagramSocket socket; public static InetAddress receiver; public static int port;
	
	public static void main( String args[] ) throws Exception
    {
        //初始化套接字、接收器和端口
        
        while(true)
        {
            sendMessage();
            receiveMessage();
        }
        
    }

	public static void sendMessage() throws IOException
	{
		//发送消息的操作
	}
	
	public static void receiveMessage() throws IOException
	{
		//接收消息的操作
	}
	
}

我想要同时运行的函数是sendMessage()receiveMessage()。目前,我正在使用一个while循环,所以程序只能在等待接收消息之前发送消息,反之亦然。

我对Java并不太熟悉,所以希望能得到一个简单的Executor Service的实现。

英文:

I'm trying to have my program be ready to send a message inputted by user and listen for an incoming message simultaneously.

I tried using Executor Service but it always gives an error that says the functions can't be static or void.

Because of this, I tried making my two functions (that I need to run simultaneously) non-static and return a String that I wouldn't do anything with. Unfortunately, I still get an error and I think it due to the fact that my functions use the static class variables declared prior.

Here's the error I get:

> EchoClient.java:66: error: <anonymous EchoClient$1> is not abstract
> and does not override abstract method call() in Callable
> callables.add(new Callable<String>() {

Here's me the code simplified and without the Executor Service:

class EchoClient
{	
    public static DatagramSocket socket; public static InetAddress receiver; public static int port;
	
	public static void main( String args[] ) throws Exception
    {
        //Initialization of socket, receiver and port
		
		while(true)
		{
			sendMessage();
			receiveMessage();
		}
		
    }

	public static void sendMessage() throws IOException
	{
		//Actions to send message
	}
	
	public static void receiveMessage() throws IOException
	{
		//Actions to receive message
	}
	
}

The functions I want to run simultaneously are sendMessage() and receiveMessage(). Right now I'm using a while loop so the program can only send a message before waiting to receive one and vice versa.

I'm really not that familiar with Java, so I'm just hoping for a simple implementation of Executor Service.

答案1

得分: 1

由于您知道您需要2个线程,您可以使用FixedThreadPool:

Executor executor = Executors.newFixedThreadPool(numberOfThreads);

要执行任务,您只需调用execute方法,将您的可运行对象作为参数传递:

executor.execute(() -> {
    while (true) {
        try {
            sendMessage();
        } catch (IOException e) {
            e.printStackTrace();
            break;
        }
    }
});
英文:

Since you know that you will need 2 Threads you can use a FixedThreadPool:

Executor executor = Executors.newFixedThreadPool(numberOfThreads);

To execute a Task you just need to call the execute methode with your runnable as a parameter:

executor.execute(() -&gt; {
	while (true) {
		try {
			sendMessage();
		} catch (IOException e) {
			e.printStackTrace();
			break;
		}
	}
});

huangapple
  • 本文由 发表于 2020年7月30日 07:04:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/63163734.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定