请问 如何让2个类中的synchronized共享同一个锁(ex21_1的对象的引用)类 ex21_1 一开始先wait(); 然后等待ex21_2执行notifyAll();后开始println;但是 我没有能够实现 卡住了 我想要ex21_1 和 ex21_2中的 synchronized run();共用一把锁--ex21_1的对象的引用。请问如何实现或者修改 谢谢。
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
public class ex21 { /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
ex21_1 a = new ex21_1();
ExecutorService exec = Executors.newCachedThreadPool();
exec.execute(a);
exec.execute(new ex21_2(a));
}}class ex21_1 implements Runnable{
public synchronized void run() {
try{wait();} catch (InterruptedException ex){}
System.out.println("1 is running");
}
}class ex21_2 implements Runnable{
private ex21_1 a;
public ex21_2(ex21_1 a){this.a = a;}
public synchronized void run(){
if(a instanceof ex21_1){
try{TimeUnit.MICROSECONDS.sleep(1);}
catch (InterruptedException ex){}
notifyAll();
}
}
}
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
public class ex21 { /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
ex21_1 a = new ex21_1();
ExecutorService exec = Executors.newCachedThreadPool();
exec.execute(a);
exec.execute(new ex21_2(a));
}}class ex21_1 implements Runnable{
public synchronized void run() {
try{wait();} catch (InterruptedException ex){}
System.out.println("1 is running");
}
}class ex21_2 implements Runnable{
private ex21_1 a;
public ex21_2(ex21_1 a){this.a = a;}
public synchronized void run(){
if(a instanceof ex21_1){
try{TimeUnit.MICROSECONDS.sleep(1);}
catch (InterruptedException ex){}
notifyAll();
}
}
}
解决方案 »
- java中 com.jme的使用
- 谁帮我把这段C代码转化为JAVA
- 如何方便地取得">"与"<"之间的字符?
- 100分只为异常,不知哪里出问题了!!!希望路过的兄弟进来帮帮忙!!小弟昨天已经弄了一晚上了!
- 小弟初学JAVA写的第一个象样的程序,希望哪位大哥能帮一把!
- xml是輿Java的关系?
- 目前已公认JAVA在PC机上比C++ 慢20倍, 请问J2em在PDA上面会比PDA的专用开发工具慢多少呢?
- 如何实现例如“下一步”这样的提示面板?
- 想了解一下学习过程?
- 解决方案咨询,求高手指点
- JAVA中关键字 ( )表示一个对象或变量一旦赋值就不能修改
- 在java中如何把double类型转换为object类型
public static Object locker=new Object();synchronized(locker)
{
//your code
}
那么。你只要能够确定 synchronized锁的对象是同一个就可以。既然你用的了2个类来实现共享
那么能共享同一把锁的事情,不外乎就是传值共享。所以public void run()
{
synchronized(x)
{
..........
}
}
这种方式比较适合
public static void main(String[] args) {
// TODO Auto-generated method stub
final Operation o = new Operation();
new Thread(new Runnable(){
public void run() {
// TODO Auto-generated method stub
o.write();
}
}).start();
new Thread(new Runnable(){
public void run() {
// TODO Auto-generated method stub
o.read();
}
}).start();
}
}
class Operation {
private boolean target = true;
public synchronized void write() {
try {
if (target) {
wait();
}
System.out.println("write()");
target = !target;
notify();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public synchronized void read() {
try {
if (!target) {
wait();
}
System.out.println("write()");
target = !target;
notify();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
package com.xh.dao;public class ThreadTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
final Operation o = new Operation();
new Thread(new Runnable(){
public void run() {
// TODO Auto-generated method stub
o.write();
}
}).start();
new Thread(new Runnable(){
public void run() {
// TODO Auto-generated method stub
o.read();
}
}).start();
}
}
class Operation {
private boolean target = true;
public synchronized void write() {
try {
while (true) {
if (target) {
wait();
}
System.out.println("write()");
target = !target;
notify();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public synchronized void read() {
try {
while (true) {
if (!target) {
wait();
}
System.out.println("read()");
target = !target;
notify();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
public class ex21 {
ex21_1 a;
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
a = new ex21_1();
ExecutorService exec = Executors.newCachedThreadPool();
exec.execute(a);
exec.execute(new ex21_2(a));
}}class ex21_1 implements Runnable{
public void run() {
synchronized(this){
try{wait();} catch (InterruptedException ex){}
System.out.println("1 is running");
}
}
}class ex21_2 implements Runnable{
private ex21_1 a;
public ex21_2(ex21_1 a){this.a = a;}
public void run(){
synchronized(a){
if(a instanceof ex21_1){
a.notifyAll();
}
}
}
}
T1 t1;
public static void main(String[] args){
Knowing k=new Knowing();
k.start();
}
public void start(){
t1=new T1();
T2 t2=new T2();
new Thread(t1).start();
new Thread(t2).start();
}
class T1 implements Runnable{ @Override
public void run() {
synchronized (this){
try {
wait();
System.out.println("Hello World!!");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
class T2 implements Runnable{ @Override
public void run() {
synchronized(t1){
t1.notifyAll();
}
}
}
}
当前线程变为对象监视器的所有者有3个方法:
1,通过执行此对象的同步实例方法。
2,通过执行在此对象上进行同步的 synchronized 语句的正文。
3,对于 Class 类型的对象,可以通过执行该类的同步静态方法。