编写一个程序,启动三个线程,三个线程的ID分别是A,B,C;,每个线程将自己的ID值在屏幕上打印10遍,打印顺序是ABCABC...
package practice;
public class ThreeThread extends Thread {
public static void main(String[] args) {
MyObject ob = new MyObject();
new Thread(new Runnable(){
@Override
public void run() {
// TODO Auto-generated method stub
for(int i = 0 ; i<10 ; i++)
ob.printA();
}
}).start();
new Thread(new Runnable(){
@Override
public void run() {
// TODO Auto-generated method stub
for(int i = 0 ; i<10 ; i++)
ob.printB();
}
}).start();
new Thread(new Runnable(){
@Override
public void run() {
// TODO Auto-generated method stub
for(int i = 0 ; i<10 ; i++)
ob.printC();
}
}).start();
}}class MyObject {
private int flag = 1;
public synchronized void printA(){
while(flag != 1){
try {
this.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.print("A");
flag = 2;
this.notifyAll();
}
public synchronized void printB(){
while(flag != 2){
try {
this.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.print("B");
flag = 3;
this.notifyAll();
}
public synchronized void printC(){
while(flag != 3){
try {
this.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.println("C");
flag = 1;
this.notifyAll();
}
}
这里为何要在每个方法前加上synchronized呢,加上synchronized不是为了同步方法吗?可是这三个线程各自调用不同的方法,不会影响其他的方法啊,求大佬解答
package practice;
public class ThreeThread extends Thread {
public static void main(String[] args) {
MyObject ob = new MyObject();
new Thread(new Runnable(){
@Override
public void run() {
// TODO Auto-generated method stub
for(int i = 0 ; i<10 ; i++)
ob.printA();
}
}).start();
new Thread(new Runnable(){
@Override
public void run() {
// TODO Auto-generated method stub
for(int i = 0 ; i<10 ; i++)
ob.printB();
}
}).start();
new Thread(new Runnable(){
@Override
public void run() {
// TODO Auto-generated method stub
for(int i = 0 ; i<10 ; i++)
ob.printC();
}
}).start();
}}class MyObject {
private int flag = 1;
public synchronized void printA(){
while(flag != 1){
try {
this.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.print("A");
flag = 2;
this.notifyAll();
}
public synchronized void printB(){
while(flag != 2){
try {
this.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.print("B");
flag = 3;
this.notifyAll();
}
public synchronized void printC(){
while(flag != 3){
try {
this.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.println("C");
flag = 1;
this.notifyAll();
}
}
这里为何要在每个方法前加上synchronized呢,加上synchronized不是为了同步方法吗?可是这三个线程各自调用不同的方法,不会影响其他的方法啊,求大佬解答
给你篇文章,理解的透透的
https://www.cnblogs.com/dreamowneryong/p/11607742.html
https://www.cnblogs.com/dreamowneryong/p/11607742.html
在这里的锁默认是对象锁
这三个方法虽然不同,但他们都有访问共同的资源,即对象锁对应对象中的flag,在访问过程中,很可能被其他线程修改
同时wait方法也必须在对象锁中调用
所以要第一个线程执行完printA以后才能第二个线程才能执行printB,printB完以后第三个线程才能执行printC,printC完以后第一个线程才能重新执行printA,依次类推