任务是使用三个线程重复打印abc,次数为我的代码为
package javap;
public class Pattern {
volatile int status=1;
public static void main(String[] args) {
Pattern p = new Pattern();
A1 a=new A1(p);
B1 b=new B1(p);
C1 c=new C1(p);
a.start();
b.start();
c.start();
}
}
class A1 extends Thread{
Pattern p1;
A1(Pattern p){
this.p1 = p;
}
@Override
public void run() {
try{
synchronized (p1) {
for (int i = 0; i < 100; i++) {
while(p1.status!=1){
p1.wait();
}
System.out.print("A ");
p1.status = 2;
p1.notifyAll();
}
}
}catch (Exception e) {
System.out.println("Exception 1 :"+e.getMessage());
}
}
}
class B1 extends Thread{
Pattern p2;
B1(Pattern p2){
this.p2 = p2;
}
@Override
public void run() {
try{
synchronized (p2) {
for (int i = 0; i < 100; i++) {
while(p2.status!=2){
p2.wait();
}
System.out.print("B ");
p2.status = 3;
p2.notifyAll();
}
}
}catch (Exception e) {
System.out.println("Exception 2 :"+e.getMessage());
}
}
}
class C1 extends Thread{
Pattern p3;
C1(Pattern p){
this.p3 = p;
}
@Override
public void run() {
try{
synchronized (p3) {
for (int i = 0; i < 100; i++) {
while(p3.status!=3){
p3.wait();
}
System.out.print("C ");
p3.status = 1;
p3.notifyAll();
}
}
}catch (Exception e) {
System.out.println("Exception 3 :"+e.getMessage());
}
}
}当我尝试使用for(;;)或while(true)时,我的ide挂起并且我正在nt获取任何输出,所以我将其限制在100次。不管怎么说,我可以让这个运行无数次。
提前感谢
发布于 2017-04-03 05:48:26
我修改了你的逻辑,它得到输出流A,B,C的间隔为1000毫秒(更好的可视化)。
public class Pattern {
volatile int status = 1;
public static void main(String[] args) {
Pattern p = new Pattern();
A1 a = new A1(p);
B1 b = new B1(p);
C1 c = new C1(p);
a.start();
b.start();
c.start();
}
}
class A1 extends Thread {
Pattern p1;
A1(Pattern p) {
this.p1 = p;
}
@Override
public void run() {
try {
synchronized (p1) {
while (true) {
while (p1.status != 1) {
try {
p1.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.print("A ");
p1.status = 2;
p1.notifyAll();
}
}
} catch (Exception e) {
System.out.println("Exception 1 :" + e.getMessage());
}
}
}
class B1 extends Thread {
Pattern p2;
B1(Pattern p2) {
this.p2 = p2;
}
@Override
public void run() {
try {
synchronized (p2) {
while (true) {
while (p2.status != 2) {
try {
p2.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.print("B ");
p2.status = 3;
p2.notifyAll();
}
}
} catch (Exception e) {
System.out.println("Exception 2 :" + e.getMessage());
}
}
}
class C1 extends Thread {
Pattern p3;
C1(Pattern p) {
this.p3 = p;
}
@Override
public void run() {
try {
synchronized (p3) {
while (true) {
while (p3.status != 3) {
try {
p3.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.print("C ");
Thread.sleep(1000);
p3.status = 1;
p3.notifyAll();
}
}
} catch (Exception e) {
System.out.println("Exception 3 :" + e.getMessage());
}
}
}产出:A、B、C.
发布于 2019-03-06 16:07:30
您还可以以以下方式解决上述问题
public class BasePatternPrintExample {
public static void main(String[] args) {
Status lock = new Status(1);
ThreadA t1 = new ThreadA("A", lock);
ThreadB t2 = new ThreadB("B", lock);
ThreadC t3 = new ThreadC("C", lock);
t1.start();
t2.start();
t3.start();
}
}地位等级
class Status{
private int status;
public Status(int status){
this.status = status;
}
public int getStatus() {
return status;
}
public void setStatus(int status) {
this.status = status;
}
}ThreadA
class ThreadA extends Thread {
private Status lock;
public ThreadA(String name, Status obj) {
super(name);
this.lock = obj;
}
public void run() {
while(true) {
synchronized (lock) {
lock.notifyAll();
//System.out.println(lock.getStatus());
while(lock.getStatus() != 1){
try {
lock.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.print("A");
lock.setStatus(2);
}
}
}
}ThreadB
class ThreadB extends Thread {
private Status lock;
public ThreadB(String name, Status obj) {
super(name);
this.lock = obj;
}
public void run() {
while(true) {
synchronized (lock) {
lock.notifyAll();
while(lock.getStatus() != 2){
try {
lock.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.print("B");
lock.setStatus(3);
}
}
}
}ThreadC
class ThreadC extends Thread {
private Status lock;
public ThreadC(String name, Status obj) {
super(name);
this.lock = obj;
}
public void run() {
while(true) {
synchronized (lock) {
lock.notifyAll();
while(lock.getStatus() != 3){
try {
lock.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.println("C");
lock.setStatus(1);
}
}
}
}发布于 2018-07-09 06:00:40
请试试这段代码
package threadexample;
class A extends Thread{
Pattern writeAbcExample;
public A(Pattern writeAbcExample) {
this.writeAbcExample = writeAbcExample;
}
@Override
public void run(){
try{
synchronized(writeAbcExample){
while(true){
while(writeAbcExample.status!=1){
try{
writeAbcExample.wait();
}catch(InterruptedException e){e.printStackTrace();}
}
System.out.println("A ");
Thread.sleep(400);
writeAbcExample.status=2;
writeAbcExample.notifyAll();
}
}
}catch(Exception e){e.printStackTrace();}
}
}
class B extends Thread{
Pattern writeAbcExample;
public B(Pattern writeAbcExample) {
this.writeAbcExample = writeAbcExample;
}
@Override
public void run(){
try{
synchronized(writeAbcExample){
while(true){
while(writeAbcExample.status!=2){
try{
writeAbcExample.wait();
}catch(InterruptedException e){e.printStackTrace();}
}
System.out.println("B ");
Thread.sleep(400);
writeAbcExample.status=3;
writeAbcExample.notifyAll();
}
}
}catch(Exception e){e.printStackTrace();}
}
}
class C extends Thread{
Pattern writeAbcExample;
public C(Pattern writeAbcExample) {
this.writeAbcExample = writeAbcExample;
}
@Override
public void run(){
try{
synchronized(writeAbcExample){
while(true){
while(writeAbcExample.status!=3){
try{
writeAbcExample.wait();
}catch(InterruptedException e){e.printStackTrace();}
}
System.out.println("C ");
Thread.sleep(400);
writeAbcExample.status=1;
writeAbcExample.notifyAll();
}
}
}catch(Exception e){e.printStackTrace();}
}
}
public class Pattern {
volatile int status=1;
public static void main(String[] args) {
Pattern writeAbcExample=new Pattern();
new A(writeAbcExample).start();
new B(writeAbcExample).start();
new C(writeAbcExample).start();
}
}https://stackoverflow.com/questions/43176198
复制相似问题