java同步和饥饿
我制作了一个程序,并期望输出如下: A 1. A. B 2. B C 3. C E 5. e 这是我的代码,我想我有饥饿问题,请帮帮我java同步和饥饿,java,multithreading,synchronization,Java,Multithreading,Synchronization,我制作了一个程序,并期望输出如下: A 1. A. B 2. B C 3. C E 5. e 这是我的代码,我想我有饥饿问题,请帮帮我 class Product { static boolean flag1, flag2, flag3; synchronized void printLwrAlpha(char value) { // System.out.println(flag3+": inside lwr_alpha"); if (!fl
class Product {
static boolean flag1, flag2, flag3;
synchronized void printLwrAlpha(char value) {
// System.out.println(flag3+": inside lwr_alpha");
if (!flag3)
try {
wait();
} catch (Exception ex) {
System.out.println(ex);
}
System.out.println(value);
flag3 = false;
flag1 = false;
System.out.println("before notify");
notify();
System.out.println("after notify");
}
synchronized void printUprAlpha(char n) {
// System.out.println(flag1+": inside upr_alpha");
if (flag1)
try {
wait();
} catch (Exception e) {
System.out.println(e);
}
System.out.println(n);
// System.out.println(num);
flag1 = true;
flag2 = true;
notify();
}
synchronized void printNum(int num) {
// System.out.println(flag2+": inside num");
if (!flag2)
try {
wait();
} catch (Exception e) {
System.out.println(e);
}
// System.out.println(n);
System.out.println(num);
flag2 = false;
flag3 = true;
notify();
}
}
class PrintNum implements Runnable {
Product p;
PrintNum(Product p) {
this.p = p;
new Thread(this, "Producer").start();
try {
Thread.sleep(1000);
} catch (Exception ex) {
System.out.println(ex);
}
}
public void run() {
for (int i = 1; i <= 5; i++)
p.printNum(i);
}
}
class PrintLwrAlpha implements Runnable {
Product p;
static char ch = 'a';
PrintLwrAlpha(Product p) {
this.p = p;
new Thread(this, "Producer").start();
try {
Thread.sleep(1000);
} catch (Exception ex) {
System.out.println(ex);
}
}
public void run() {
for (int i = 1; i <= 5; i++) {
char c = (char) (ch + (i - 1));
p.printLwrAlpha(c);
}
}
}
class PrintUprAlpha implements Runnable {
Product p;
static char ch = 'A';
PrintUprAlpha(Product p) {
this.p = p;
new Thread(this, "Producer").start();
try {
Thread.sleep(1000);
} catch (Exception ex) {
System.out.println(ex);
}
}
public void run() {
for (int i = 1; i <= 5; i++) {
char c = (char) (ch + (i - 1));
p.printUprAlpha(c);
}
}
}
public class MainClass1 {
public static void main(String ar[]) {
Product p = new Product();
new PrintNum(p);
new PrintUprAlpha(p);
new PrintLwrAlpha(p);
}
}
类产品{
静态布尔flag1、flag2、flag3;
同步无效打印LWRALPHA(字符值){
//系统输出打印LN(flag3+“:内部lwr_alpha”);
如果(!flag3)
试一试{
等待();
}捕获(例外情况除外){
系统输出打印项次(ex);
}
系统输出打印项次(值);
flag3=假;
flag1=假;
System.out.println(“通知前”);
通知();
System.out.println(“通知后”);
}
同步的无效打印字母(字符n){
//系统输出打印LN(flag1+“:内部upr_α”);
如果(标志1)
试一试{
等待();
}捕获(例外e){
系统输出打印ln(e);
}
系统输出println(n);
//系统输出打印项数(num);
flag1=真;
flag2=真;
通知();
}
同步的void printNum(int num){
//System.out.println(flag2+“:insidenum”);
如果(!flag2)
试一试{
等待();
}捕获(例外e){
系统输出打印ln(e);
}
//系统输出println(n);
系统输出打印项数(num);
flag2=假;
flag3=真;
通知();
}
}
类PrintNum实现可运行{
产品p;
PrintNum(产品p){
这个,p=p;
新线程(此“生产者”).start();
试一试{
睡眠(1000);
}捕获(例外情况除外){
系统输出打印项次(ex);
}
}
公开募捐{
对于(inti=1;i替换所有的if,例如
if (!flag3)
带while循环
while (!flag3)
如果我理解正确的话,您的问题是您试图将单个等待对象与多个线程一起使用。等待/通知的常见场景如下:一个线程等待资源变为可用,而第二个线程生成资源并通知第一个线程。
在代码中,它可能如下所示:
class ResourceFactory {
public synchronized void produce()
{
// make the resource available
obj.notify();
}
public synchronized void consume()
{
if( /* resource is not available */ ) {
obj.wait();
}
// do something with resource
}
}
// thread 1
obj1.wait();
obj2.notify()
// thread 2
obj2.wait();
obj3.notify()
// thread 3
obj3.wait();
obj1.notify()
当多个线程试图在单个对象上等待时,问题在于由实现决定在notify调用后哪个线程将被唤醒。我认为您应该创建3个不同的对象,并执行如下操作:
class ResourceFactory {
public synchronized void produce()
{
// make the resource available
obj.notify();
}
public synchronized void consume()
{
if( /* resource is not available */ ) {
obj.wait();
}
// do something with resource
}
}
// thread 1
obj1.wait();
obj2.notify()
// thread 2
obj2.wait();
obj3.notify()
// thread 3
obj3.wait();
obj1.notify()
小心,尽量不要使代码死锁
最后是您的代码。前两个线程正在等待并相互唤醒,尽管有标记。当第三个线程被唤醒时,没有线程通知它。因此这是一个典型的死锁。请在粘贴之前正确缩进您的代码(在eclipse中,Ctrl+Shift+F
这些标志是什么意思?首先描述一下你的目标。我们应该在哪些方面帮助你?@Alexei Kaigorodov我的目标是这样打印:A 1 A B 2 B C 3 C…E 5e@user2833479然后在单个线程中执行。使用多线程执行顺序任务是一种反常行为,请直接告诉你的教授。