public class Test {
private static class Counter {
public synchronized void count() {
for (int i = 0; i < 6; i++) {
System.out.println(Thread.currentThread().getName() + ", i = " + i);
}
}
}
private static class MyThread extends Thread {
private Counter mCounter;
public MyThread(Counter counter) {
mCounter = counter;
}
@Override
public void run() {
super.run();
mCounter.count();
}
}
public static void main(String[] var0) {
Counter counter = new Counter();
// 注:myThread1 和 myThread2 是调用同一个对象 counter
MyThread myThread1 = new MyThread(counter);
MyThread myThread2 = new MyThread(counter);
myThread1.start();
myThread2.start();
}
}
Thread-0, i = 0 Thread-0, i = 1 Thread-0, i = 2 Thread-0, i = 3 Thread-0, i = 4 Thread-0, i = 5 Thread-1, i = 0 Thread-1, i = 1 Thread-1, i = 2 Thread-1, i = 3 Thread-1, i = 4 Thread-1, i = 5
public class Test {
private static class Counter {
public void count() {
synchronized (this) {
for (int i = 0; i < 6; i++) {
System.out.println(Thread.currentThread().getName() + ", i = " + i);
}
}
}
}
private static class MyThread extends Thread {
private Counter mCounter;
public MyThread(Counter counter) {
mCounter = counter;
}
@Override
public void run() {
super.run();
mCounter.count();
}
}
public static void main(String[] var0) {
Counter counter = new Counter();
MyThread myThread1 = new MyThread(counter);
MyThread myThread2 = new MyThread(counter);
myThread1.start();
myThread2.start();
}
}
Thread-0, i = 0 Thread-0, i = 1 Thread-0, i = 2 Thread-0, i = 3 Thread-0, i = 4 Thread-0, i = 5 Thread-1, i = 0 Thread-1, i = 1 Thread-1, i = 2 Thread-1, i = 3 Thread-1, i = 4 Thread-1, i = 5
public static void main(String[] var0) {
// 注意:myThread1 和 myThread2 传入的 Counter 是两个不同的对象
MyThread myThread1 = new MyThread(new Counter());
MyThread myThread2 = new MyThread(new Counter());
myThread1.start();
myThread2.start();
}
Thread-0, i = 0 Thread-1, i = 0 Thread-0, i = 1 Thread-1, i = 1 Thread-1, i = 2 Thread-1, i = 3 Thread-0, i = 2 Thread-1, i = 4 Thread-0, i = 3 Thread-1, i = 5 Thread-0, i = 4 Thread-0, i = 5
public class Test {
private static class Counter {
public synchronized void count() {
System.out.println(Thread.currentThread().getName() + " sleep");
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + " awake");
}
public synchronized void doOtherThings(){
System.out.println(Thread.currentThread().getName() + " doOtherThings");
}
}
public static void main(String[] var0) {
final Counter counter = new Counter();
new Thread(new Runnable() {
@Override
public void run() {
counter.count();
}
}).start();
new Thread(new Runnable() {
@Override
public void run() {
counter.doOtherThings();
}
}).start();
}
}
Thread-0 sleep Thread-0 awake Thread-1 doOtherThings
public void doOtherThings(){
synchronized (this){
System.out.println(Thread.currentThread().getName() + " doOtherThings");
}
}
Thread-0 sleep Thread-0 awake Thread-1 doOtherThings
// 这两个 synchronized 锁的是同一个对象
public synchronized void count(){};
public void doOtherThings(){
synchronized (this){}
}
public void doOtherThings(){
System.out.println(Thread.currentThread().getName() + " doOtherThings");
}
Thread-0 sleep Thread-1 doOtherThings Thread-0 awake
public class Test {
private static class Counter {
public static synchronized void count() {
System.out.println(Thread.currentThread().getName() + " sleep");
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + " awake");
}
public static synchronized void doOtherThings(){
System.out.println(Thread.currentThread().getName() + " doOtherThings");
}
}
public static void main(String[] var0) {
new Thread(new Runnable() {
@Override
public void run() {
Counter.count();
}
}).start();
new Thread(new Runnable() {
@Override
public void run() {
Counter.doOtherThings();
}
}).start();
}
}
Thread-0 sleep Thread-0 awake Thread-1 doOtherThings
public class Test {
private static class Counter {
public static synchronized void count() {
System.out.println(Thread.currentThread().getName() + " sleep");
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + " awake");
}
public void doOtherThings(){
synchronized (Counter.class){
System.out.println(Thread.currentThread().getName() + " doOtherThings");
}
}
}
public static void main(String[] var0) {
new Thread(new Runnable() {
@Override
public void run() {
Counter.count();
}
}).start();
new Thread(new Runnable() {
@Override
public void run() {
Counter counter = new Counter();
counter.doOtherThings();
}
}).start();
}
}
Thread-0 sleep Thread-0 awake Thread-1 doOtherThings
public class Test {
private static class Counter {
public void count() {
synchronized (this){
System.out.println(Thread.currentThread().getName() + " sleep");
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + " awake");
}
}
public void doOtherThings(){
synchronized (Counter.class){
System.out.println(Thread.currentThread().getName() + " doOtherThings");
}
}
}
public static void main(String[] var0) {
final Counter counter = new Counter();
new Thread(new Runnable() {
@Override
public void run() {
counter.count();
}
}).start();
new Thread(new Runnable() {
@Override
public void run() {
counter.doOtherThings();
}
}).start();
}
}
Thread-0 sleep Thread-1 doOtherThings Thread-0 awake
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有