1、doSome方法执行需要等待dother方法执行吗
不需要
package com;
class test {
public static void main(String[] args) thorows InterruptedExcption {
//创建对象
MyClass mc = new MyClass();
//创建线程对象
MyThread m = new MyThread(mc);
MyThread m1 = new MyThread(mc);
//设置线程名字
m.setName("t1");
m1.setName("t2");
//启动
m.start();
//这个睡眠是保证t1线程先执行
Thread.sleep(1000);
m1.start();
}
}
class MyThread extends Thread {
private MyClass mc;
public MyThread(MyClass mc) {
this.mc = mc;
}
public void run() {
if (Thread.currentThread().getName().equals("t1")) {
mc.doSome();
}
if (Thread.currentThread().getName().equals("t2")){
mc.doOther();
}
}
}
class MyClass {
public synchronized void doSome() {
System.out.println("doSome begin");
try {
Thread.sleep(1000 * 10);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("doSome over");
}
public void doOther() {
System.out.println("doOther begin");
System.out.println("doOther over");
}
}
2、doSome方法执行需要等待doOther方法吗
需要。2把类锁
package com;
class test {
public static void main(String[] args) thorows InterruptedExcption {
//创建对象
MyClass mc = new MyClass();
//创建线程对象
MyThread m = new MyThread(mc);
MyThread m1 = new MyThread(mc);
//设置线程名字
m.setName("t1");
m1.setName("t2");
//启动
m.start();
//这个睡眠是保证t1线程先执行
Thread.sleep(1000);
m1.start();
}
}
class MyThread extends Thread {
private MyClass mc;
public MyThread(MyClass mc) {
this.mc = mc;
}
public void run() {
if (Thread.currentThread().getName().equals("t1")) {
mc.doSome();
}
if (Thread.currentThread().getName().equals("t2")){
mc.doOther();
}
}
}
class MyClass {
public synchronized void doSome() {
System.out.println("doSome begin");
try {
Thread.sleep(1000 * 10);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("doSome over");
}
public synchronized void doOther() {
System.out.println("doOther begin");
System.out.println("doOther over");
}
}
3、doSome方法执行需要doOther方法结束吗
不需要,因为Myclass是2个对象,2把锁
package com;
class test {
public static void main(String[] args) {
//创建对象
MyClass mc = new MyClass();
MyClass mc1 = new MyClass();
//创建线程对象
MyThread m = new MyThread(mc);
MyThread m1 = new MyThread(mc1);
//设置线程名字
m.setName("t1");
m1.setName("t2");
//启动
m.start();
m1.start();
}
}
class MyThread extends Thread {
private MyClass mc;
public MyThread(MyClass mc) {
this.mc = mc;
}
public void run() {
if (Thread.currentThread().getName().equals("t1")) {
mc.doSome();
}
if (Thread.currentThread().getName().equals("t2")){
mc.doOther();
}
}
}
class MyClass {
public synchronized void doSome() {
System.out.println("doSome begin");
try {
Thread.sleep(1000 * 10);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("doSome over");
}
public synchronized void doOther() {
System.out.println("doOther begin");
System.out.println("doOther over");
}
}
4、doSome方法执行需要doOther方法结束吗
需要,synchronized 在静态方法上找类锁
package com;
class test {
public static void main(String[] args) {
//创建对象
MyClass mc = new MyClass();
MyClass mc1 = new MyClass();
//创建线程对象
MyThread m = new MyThread(mc);
MyThread m1 = new MyThread(mc1);
//设置线程名字
m.setName("t1");
m1.setName("t2");
//启动
m.start();
m1.start();
}
}
class MyThread extends Thread {
private MyClass mc;
public MyThread(MyClass mc) {
this.mc = mc;
}
public void run() {
if (Thread.currentThread().getName().equals("t1")) {
mc.doSome();
}
if (Thread.currentThread().getName().equals("t2")){
mc.doOther();
}
}
}
class MyClass {
public synchronized static void doSome() {
System.out.println("doSome begin");
try {
Thread.sleep(1000 * 10);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("doSome over");
}
public synchronized static void doOther() {
System.out.println("doOther begin");
System.out.println("doOther over");
}
}