多线程8锁问题
问题:
- 两个线程之间睡眠1秒,先打印哪个?
public class Demo1 {
public static void main(String[] args) throws InterruptedException {
Test1 test1 = new Test1();
new Thread(()->{ test1.one(); },"a").start();
TimeUnit.SECONDS.sleep(1);
new Thread(()->{ test1.two(); },"b").start();
}
}
class Test1{
synchronized void one(){ System.out.println("one"); }
synchronized void two(){ System.out.println("two"); }
}
结果
one
two
- 在方法one睡眠5秒,先打印哪个?
public class Demo2 {
public static void main(String[] args) throws InterruptedException {
Test2 test2 = new Test2();
new Thread(()->{
try {
test2.one();
} catch (InterruptedException e) {
e.printStackTrace();
} },"a").start();
TimeUnit.SECONDS.sleep(1);
new Thread(()->{
test2.two(); },"b").start();
}
}
class Test2{
synchronized void one() throws InterruptedException {
TimeUnit.SECONDS.sleep(5);
System.out.println("one");
}
synchronized void two(){ System.out.println("two"); }
}
结果
one
two
- 新增一个普通的hello方法,先打印哪个
public class Demo3 {
public static void main(String[] args) throws InterruptedException {
Test3 test3 = new Test3();
new Thread(()->{
try {
test3.one();
} catch (InterruptedException e) {
e.printStackTrace();
}
},"a").start();
TimeUnit.SECONDS.sleep(1);
new Thread(()->{
test3.hello();
},"b").start();
}
}
class Test3{
synchronized void one() throws InterruptedException {
TimeUnit.SECONDS.sleep(5);
System.out.println("one");
}
void hello(){
System.out.println("hello");
}
}
结果
hello
one
- 两个不同的对象,先打印哪个?
public class Demo4 {
public static void main(String[] args) throws InterruptedException {
Test4 test4 = new Test4();
Test4 test5_ = new Test4();
new Thread(()->{
try {
test4.one();
} catch (InterruptedException e) {
e.printStackTrace();
}
},"a").start();
TimeUnit.SECONDS.sleep(1);
new Thread(()->{
test4_.two();
},"b").start();
}
}
class Test4{
synchronized void one() throws InterruptedException {
TimeUnit.SECONDS.sleep(5);
System.out.println("one");
}
synchronized void two(){
System.out.println("two");
}
}
结果
two
one
- 一个普通同步方法,一个静态同步方法,一个对象,先打印哪个?
public class Demo5{
public static void main(String[] args) throws InterruptedException {
Test5 test5 = new Test5();
new Thread(()->{
try {
test5.one();
} catch (InterruptedException e) {
e.printStackTrace();
}
},"a").start();
TimeUnit.SECONDS.sleep(1);
new Thread(()->{
test5.two();
},"b").start();
}
}
class Test5{
synchronized void one() throws InterruptedException {
TimeUnit.SECONDS.sleep(5);
System.out.println("one");
}
static synchronized void two(){
System.out.println("two");
}
}
结果
two
one
- 两个静态同步方法,一个对象,先打印哪个?
public class Demo6{
public static void main(String[] args) throws InterruptedException {
Test6 test6 = new Test6();
new Thread(()->{
try {
test6.one();
} catch (InterruptedException e) {
e.printStackTrace();
}
},"a").start();
TimeUnit.SECONDS.sleep(1);
new Thread(()->{
test6.two();
},"b").start();
}
}
class Test6{
static synchronized void one() throws InterruptedException {
TimeUnit.SECONDS.sleep(5);
System.out.println("one");
}
static synchronized void two(){
System.out.println("two");
}
}
结果
one
two
- 两个静态同步问题,两个对象,先打印哪个?
public class Demo7 {
public static void main(String[] args) throws InterruptedException {
Test7 test7 = new Test7();
Test7 test7_ = new Test7();
new Thread(()->{
try {
test7.one();
} catch (InterruptedException e) {
e.printStackTrace();
}
},"a").start();
TimeUnit.SECONDS.sleep(1);
new Thread(()->{
test7_.two();
},"b").start();
}
}
class Test7{
static synchronized void one() throws InterruptedException {
TimeUnit.SECONDS.sleep(5);
System.out.println("one");
}
static synchronized void two(){
System.out.println("two");
}
}
结果
one
two
- 一个普通同步方法,一个静态同步方法,两个对象,先打印哪个?
public class Demo8 {
public static void main(String[] args) throws InterruptedException {
Test8 test8 = new Test8();
Test8 test8_ = new Test8();
new Thread(()->{
try {
test8one();
} catch (InterruptedException e) {
e.printStackTrace();
}
},"a").start();
TimeUnit.SECONDS.sleep(1);
new Thread(()->{
test8_.two();
},"b").start();
}
}
class Test8{
static synchronized void one() throws InterruptedException {
TimeUnit.SECONDS.sleep(5);
System.out.println("one");
}
synchronized void two(){
System.out.println("two");
}
}
结果
two
one