package exam;
public class exam01 {
public static void main(String[] args) {
Myclass mm = new Myclass();
MyThread a = new MyThread(mm);
MyThread b = new MyThread(mm);
a.setName("a");
b.setName("b");
a.start();
b.start();
/**
* 问doSome()执行的时候,doOther()方法会不会等待?
* 为什么?
* 不需要,分析一下,synchronized方法出现在实例方法上表示锁的是this
* 因为doOther方法上没有synchronized,没有锁,doOther不需要这把锁
* */
}
}
class MyThread extends Thread{
Myclass myclass;
public MyThread(Myclass myclass){
this.myclass = myclass;
}
public MyThread() {
}
@Override
public void run() {
if(Thread.currentThread().getName().equals("a")){
myclass.doSome();
}
else if(Thread.currentThread().getName().equals("b")){
myclass.doOther();
}
}
}
class Myclass{
public synchronized void doSome(){
System.out.println("doSome()begin");
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("doSome()over!!!");
}
public void doOther(){
System.out.println("doOther()begin");
System.out.println("doOther()over!!!");
}
}
package exam02;
public class exam01 {
public static void main(String[] args) {
Myclass mm = new Myclass();
MyThread a = new MyThread(mm);
MyThread b = new MyThread(mm);
a.setName("a");
b.setName("b");
a.start();
b.start();
/**
* 问doSome()执行的时候,doOther()方法会不会等待?
* 这里doOther()方法上加了个synchronized
* 这就需要等待了,因为他要去找那把锁,而锁被占了
* */
}
}
class MyThread extends Thread{
Myclass myclass;
public MyThread(Myclass myclass){
this.myclass = myclass;
}
public MyThread() {
}
@Override
public void run() {
if(Thread.currentThread().getName().equals("a")){
myclass.doSome();
}
else if(Thread.currentThread().getName().equals("b")){
myclass.doOther();
}
}
}
class Myclass{
public synchronized void doSome(){
System.out.println("doSome()begin");
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("doSome()over!!!");
}
public synchronized void doOther(){
System.out.println("doOther()begin");
System.out.println("doOther()over!!!");
}
}
package exam03;
public class exam01 {
public static void main(String[] args) {
Myclass mm = new Myclass();
Myclass mm02 = new Myclass();
MyThread a = new MyThread(mm);
MyThread b = new MyThread(mm02);
a.setName("a");
b.setName("b");
a.start();
b.start();
/**
* 问doSome()执行的时候,doOther()方法会不会等待?
* 这里doOther()方法上加了个synchronized
* 这里又new一个Myclass对象出来,需不需要等待?
* 不需要,因为两个对象两把锁,a拿了mm的锁,b拿了mm02的锁,不需要等待
* */
}
}
class MyThread extends Thread{
Myclass myclass;
public MyThread(Myclass myclass){
this.myclass = myclass;
}
public MyThread() {
}
@Override
public void run() {
if(Thread.currentThread().getName().equals("a")){
myclass.doSome();
}
else if(Thread.currentThread().getName().equals("b")){
myclass.doOther();
}
}
}
class Myclass{
public synchronized void doSome(){
System.out.println("doSome()begin");
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("doSome()over!!!");
}
public synchronized void doOther(){
System.out.println("doOther()begin");
System.out.println("doOther()over!!!");
}
}
package exam04;
public class exam01 {
public static void main(String[] args) {
Myclass mm = new Myclass();
Myclass mm02 = new Myclass();
MyThread a = new MyThread(mm);
MyThread b = new MyThread(mm);
a.setName("a");
b.setName("b");
a.start();
b.start();
/**
* 问doSome()执行的时候,doOther()方法会不会等待?
* 这里doOther()方法上加了个synchronized
* 这里又new一个Myclass对象出来,
* 这里又将doSome和doOther方法上改成static的(静态方法锁的是类锁,类锁只有一把)
* 所以这里就算new了两个对象,也还是需要等待的
*
* 如果只在doSome上加了static的话,doOther方法是不需要等待的(无论b和a是不是共享一个对象)
*
*
*/
}
}
class MyThread extends Thread{
Myclass myclass;
public MyThread(Myclass myclass){
this.myclass = myclass;
}
public MyThread() {
}
@Override
public void run() {
if(Thread.currentThread().getName().equals("a")){
myclass.doSome();
}
else if(Thread.currentThread().getName().equals("b")){
myclass.doOther();
}
}
}
class Myclass{
public synchronized static void doSome(){
System.out.println("doSome()begin");
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("doSome()over!!!");
}
public synchronized void doOther(){
System.out.println("doOther()begin");
/* try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}*/
System.out.println("doOther()over!!!");
}
}