java - Java - 如何为多个线程同步运行函数
问题描述
我必须使用Runnable
命名类的三个实例,并且对所有三个实例changeCase
只使用一个类的单个实例。流程执行需要连续运行,这就是我在覆盖方法中使用的原因。每个线程的方法执行是重叠的。我该如何解决这个问题?Server
changeCase
while (true)
run
process
我的代码:
package uppercase;
import java.util.Scanner;
class Server {
void process(String threadName) {
System.out.println("You are inside the Server " +threadName);
System.out.println("Enter your sentence");
Scanner sc1 = new Scanner(System.in);
String input = sc1.nextLine();
System.out.println("Press 1 to convert to Uppercase or Press 2 to convert to Lowercase");
Scanner sc2 = new Scanner(System.in);
int num = sc2.nextInt();
switch (num) {
case 1:
changetoUpperCase(input);
break;
case 2:
changetoLowerCase(input);
break;
default:
System.out.println("Invalid number given\n\n");
break;
}
}
void changetoUpperCase(String msg) {
printMessage(msg.toUpperCase());
}
void changetoLowerCase(String msg) {
printMessage(msg.toLowerCase());
}
void printMessage(String msg) {
System.out.println("[ " +msg+ " ]\n\n");
try {
Thread.sleep(1000);
}catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
class changeCase implements Runnable {
String threadName;
Server target;
Thread t;
public changeCase(Server c, String s) {
this.threadName = s;
this.target = c;
}
@Override
synchronized public void run() {
while(true) {
target.process(threadName);
}
//target.process(threadName);
}
}
public class UpperCase {
public static void main(String[] args) {
Server s1 = new Server();
Thread t1 = new Thread(new changeCase(s1, "Thread1"));
Thread t2 = new Thread(new changeCase(s1, "Thread2"));
Thread t3 = new Thread(new changeCase(s1, "Thread3"));
t1.start();
t2.start();
t3.start();
}
}
我需要的输出:
run:
You are inside the Server Thread1
Enter your sentence
hello world
Press 1 to convert to Uppercase or Press 2 to convert to Lowercase
1
[ HELLO WORLD ]
You are inside the Server Thread3
Enter your sentence
HELLO WORLD
Press 1 to convert to Uppercase or Press 2 to convert to Lowercase
2
[ hello world ]
You are inside the Server Thread2
Enter your sentence
hello world
Press 1 to convert to Uppercase or Press 2 to convert to Lowercase
4
Invalid number given
You are inside the Server Thread3
Enter your sentence
我得到的输出:
run:
You are inside the Server Thread1
Enter your sentence
You are inside the Server Thread2
Enter your sentence
You are inside the Server Thread3
Enter your sentence
解决方案
您可以使用计数器使线程一个一个地执行。
class Server {
int threadCount = 3;
volatile int i = 1;
synchronized void process(String threadName) {
int thread = Integer.parseInt(threadName.substring(threadName.length() - 1));
if (thread % threadCount != i) {
return;
}
i = i == threadCount - 1 ? 0 : i + 1;
System.out.println("You are inside the Server " + threadName);
System.out.println("Enter your sentence");
Scanner sc1 = new Scanner(System.in);
String input = sc1.nextLine();
...
推荐阅读
- magento2 - Magento2上的面包屑后如何从cms页面移动页面标题
- twilio-flex - 如何为生产和暂存 Twilio Flex 项目设置不同的插件配置?
- python - Telethon Python TypeError:__init__() 得到了一个意外的关键字参数“哈希”
- python - Python中特征向量对应的eugenvalues
- sql - 使用两列约束 SQL 按最大日期过滤
- android - 如何在 android 中向 AppBarLayout 添加按钮?
- python - Django:会话变量-> 如何在不破坏 django 身份验证核心的情况下覆盖注销
- python-3.x - Python 仅对“py”命令作出反应 - 如何恢复为标准?
- wso2 - 在 WSO2 IS 中用户自行注册期间如何发送电子邮件?
- eclipse - 尝试在 Eclipse 上运行标准谷歌应用引擎程序时出错