62,620
社区成员
发帖
与我相关
我的任务
分享
class A {
String zz = "start";
Object obj = new Object(); //用于锁的对象
public synchrinized void synSetZZ(String z) { //这里是锁方法
try {
for (int i=0; i<10; i++) {
System.out.println(Thread.currentThread().getName()+ "->" + i);
Thread.sleep();
}
} catch (Throwable e) {
e.printStackTrace();
}
zz = z;
System.out.println(Thread.currentThread().getName()+ "->" + zz);
}
public void setZZ(String z) {
try {
for (int i=0; i<10; i++) {
System.out.println(Thread.currentThread().getName()+ "->" + i);
Thread.sleep();
}
} catch (Throwable e) {
e.printStackTrace();
}
//为了说明synchronized method 跟syncronized(this)不一样,在前面加入了上面的代码
//可以知道,在synchronized(this)之前,多个线程可以同时进入方法
synchronized(this) { //这里是锁自己
zz = z;
System.out.println(Thread.currentThread().getName() + "->" + zz);
}
}
public void synObjSetZZ(String z) {
synchronized(obj) { //这里是锁obj
try {
for (int i=0; i<10; i++) {
System.out.println(Thread.currentThread().getName()+ "->" + i);
Thread.sleep();
}
} catch (Throwable e) {
e.printStackTrace();
}
zz = z;
System.out.println(Thread.currentThread().getName() + "->" + zz);
}
}
}
class B implements Runnable {
public static int runMode = 1;
A a;
public B (A a) {
this.a = a;
}
public void run () {
if (runMode == 1) {
a.synSetZZ("Mode:" + 1);
} else if (runMode == 2) {
a.setZZ("Mode:" + 2);
} else {
a.synObjSetZZ("Mode:" + 3);
}
}
}
public class Test {
public static void main(String[] args) {
A a = new A();
B b = new B(a);
System.out.println("--------Mode 1-----------");
Thread t1 = new Thread(b);
Thread t2 = new Thread(b);
t1.start();
t2.start();
System.out.println("--------Mode 2-----------");
B.runMode = 2;
t1 = new Thread(b);
t2 = new Thread(b);
t1.start();
t2.start();
System.out.println("--------Mode 3-----------");
B.runMode = 3;
t1 = new Thread(b);
t2 = new Thread(b);
t1.start();
t2.start();
}
}
//这个上面的,我稍微改了点,和修正了些语法错误
class C {
private String zz = "Nothing happend";
private Object obj = new Object();
public String getString() {
return zz;
}
public void setString() {
synchronized (obj) {
zz = Thread.currentThread().getName() + " change its context!";
System.out.println(zz);
}
}
}
public class Test implements Runnable {
private C c = new C();
public static void main(String[] args) {
Test a = new Test();
Thread one = new Thread(a);
Thread two = new Thread(a);
one.setName("OneThread");
two.setName("TwoThread");
one.start();
two.start();
}
public void run() {
for (int i = 0; i < 10; i++) {
c.setString();
}
}
}
//这个是输出
OneThread change its context!
TwoThread change its context!
OneThread change its context!
TwoThread change its context!
OneThread change its context!
TwoThread change its context!
TwoThread change its context!
TwoThread change its context!
TwoThread change its context!
TwoThread change its context!
TwoThread change its context!
OneThread change its context!
TwoThread change its context!
OneThread change its context!
TwoThread change its context!
OneThread change its context!
OneThread change its context!
OneThread change its context!
OneThread change its context!
OneThread change its context!
class C {
private String zz = "Nothing happend";
private Object obj = new Object();
public String getString() {
return zz;
}
public void setString() {
synchronized (obj) {
for (int i = 0; i < 10; i++) {
zz = Thread.currentThread().getName() + " change its context!";
System.out.println(zz);
}
}
}
}
public class Test implements Runnable {
private C c = new C();
public static void main(String[] args) {
Test a = new Test();
Thread one = new Thread(a);
Thread two = new Thread(a);
one.setName("OneThread");
two.setName("TwoThread");
one.start();
two.start();
}
public void run() {
c.setString();
}
}
class C {
private String zz = "Nothing happend";
public String getString(){
return zz;
}
public void setString(){
synchorized(XXX){
zz = Thread.currentThread().getName() + "change its context!";
}
}
}
class XXX{
}
public class A implements Runnable{
private C c = new C();
public static void main(String [] args){
A a = new a();
Thread one = new Thread(a);
Thread two= new Thread(a);
one.setName("OneThread");
two.setName("TwoThread");
one.start();
two.start();
}
public void run(){
for(int i=0 ;i<10;<i++)
{
c.setString();
}
}
}