class MyRunable implements Runnable {
@Override
public void run() {
synchronized(this) {
try {
for (int i = 0; i < 5; i++) {
Thread.sleep(); // 休眠ms
System.out.println(Thread.currentThread().getName() + " loop " + i);
}
} catch (InterruptedException ie) {
}
}
}
}
public class Demo_ {
public static void main(String[] args) {
Runnable demo = new MyRunable(); // 新建“Runnable对象”
Thread t1 = new Thread(demo, "t1"); // 新建“线程t1”, t1是基于demo这个Runnable对象
Thread t2 = new Thread(demo, "t2"); // 新建“线程t2”, t2是基于demo这个Runnable对象
t.start(); // 启动“线程t”
t.start(); // 启动“线程t”
}
}
class MyThread extends Thread {
public MyThread(String name) {
super(name);
}
@Override
public void run() {
synchronized(this) {
try {
for (int i = 0; i < 5; i++) {
Thread.sleep(100); // 休眠100ms
System.out.println(Thread.currentThread().getName() + " loop " + i);
}
} catch (InterruptedException ie) {
}
}
}
}
public class Demo1_2 {
public static void main(String[] args) {
Thread t1 = new MyThread("t1"); // 新建“线程t1”
Thread t2 = new MyThread("t2"); // 新建“线程t2”
t.start(); // 启动“线程t”
t.start(); // 启动“线程t”
}
}
class Count {
// 含有synchronized同步块的方法
public void synMethod() {
synchronized(this) {
try {
for (int i = 0; i < 5; i++) {
Thread.sleep(100); // 休眠100ms
System.out.println(Thread.currentThread().getName() + " synMethod loop " + i);
}
} catch (InterruptedException ie) {
}
}
}
// 非同步的方法
public void nonSynMethod() {
try {
for (int i = 0; i < 5; i++) {
Thread.sleep(100);
System.out.println(Thread.currentThread().getName() + " nonSynMethod loop " + i);
}
} catch (InterruptedException ie) {
}
}
}
public class Demo {
public static void main(String[] args) {
final Count count = new Count();
// 新建t, t会调用“count对象”的synMethod()方法
Thread t = new Thread(
new Runnable() {
@Override
public void run() {
count.synMethod();
}
}, "t1");
// 新建t2, t2会调用“count对象”的nonSynMethod()方法
Thread t2 = new Thread(
new Runnable() {
@Override
public void run() {
count.nonSynMethod();
}
}, "t2");
t1.start(); // 启动t1
t2.start(); // 启动t2
}
}
class Count {
// 含有synchronized同步块的方法
public void synMethod() {
synchronized(this) {
try {
for (int i = 0; i < 5; i++) {
Thread.sleep(); // 休眠ms
System.out.println(Thread.currentThread().getName() + " synMethod loop " + i);
}
} catch (InterruptedException ie) {
}
}
}
// 也包含synchronized同步块的方法
public void nonSynMethod() {
synchronized(this) {
try {
for (int i = 0; i < 5; i++) {
Thread.sleep(100);
System.out.println(Thread.currentThread().getName() + " nonSynMethod loop " + i);
}
} catch (InterruptedException ie) {
}
}
}
}
public class Demo3 {
public static void main(String[] args) {
final Count count = new Count();
// 新建t1, t1会调用“count对象”的synMethod()方法
Thread t1 = new Thread(
new Runnable() {
@Override
public void run() {
count.synMethod();
}
}, "t1");
// 新建t2, t2会调用“count对象”的nonSynMethod()方法
Thread t2 = new Thread(
new Runnable() {
@Override
public void run() {
count.nonSynMethod();
}
}, "t2");
t1.start(); // 启动t1
t2.start(); // 启动t2
}
}
public synchronized void foo1() {
System.out.println("synchronized methoed");
}
public void foo2() {
synchronized (this) {
System.out.println("synchronized methoed");
}
}
// Demo4.java的源码
public class Demo4 {
public synchronized void synMethod() {
for(int i=0; i<1000000; i++)
;
}
public void synBlock() {
synchronized( this ) {
for(int i=0; i<1000000; i++)
;
}
}
public static void main(String[] args) {
Demo4 demo = new Demo4();
long start, diff;
start = System.currentTimeMillis(); // 获取当前时间(millis)
demo.synMethod(); // 调用“synchronized方法”
diff = System.currentTimeMillis() - start; // 获取“时间差值”
System.out.println("synMethod() : "+ diff);
start = System.currentTimeMillis(); // 获取当前时间(millis)
demo.synBlock(); // 调用“synchronized方法块”
diff = System.currentTimeMillis() - start; // 获取“时间差值”
System.out.println("synBlock() : "+ diff);
}
}
pulbic class Something {
public synchronized void isSyncA(){}
public synchronized void isSyncB(){}
public static synchronized void cSyncA(){}
public static synchronized void cSyncB(){}
}
// LockTest1.java的源码
class Something {
public synchronized void isSyncA(){
try {
for (int i = 0; i < 5; i++) {
Thread.sleep(); // 休眠ms
System.out.println(Thread.currentThread().getName()+" : isSyncA");
}
}catch (InterruptedException ie) {
}
}
public synchronized void isSyncB(){
try {
for (int i = 0; i < 5; i++) {
Thread.sleep(100); // 休眠100ms
System.out.println(Thread.currentThread().getName()+" : isSyncB");
}
}catch (InterruptedException ie) {
}
}
}
public class LockTest {
Something x = new Something();
Something y = new Something();
// 比较(01) x.isSyncA()与x.isSyncB()
private void test1() {
// 新建t11, t11会调用 x.isSyncA()
Thread t11 = new Thread(
new Runnable() {
@Override
public void run() {
x.isSyncA();
}
}, "t11");
// 新建t12, t12会调用 x.isSyncB()
Thread t12 = new Thread(
new Runnable() {
@Override
public void run() {
x.isSyncB();
}
}, "t12");
t11.start(); // 启动t11
t12.start(); // 启动t12
}
public static void main(String[] args) {
LockTest1 demo = new LockTest1();
demo.test1();
}
}
// LockTest2.java的源码
class Something {
public synchronized void isSyncA(){
try {
for (int i = 0; i < 5; i++) {
Thread.sleep(100); // 休眠100ms
System.out.println(Thread.currentThread().getName()+" : isSyncA");
}
}catch (InterruptedException ie) {
}
}
public synchronized void isSyncB(){
try {
for (int i = 0; i < 5; i++) {
Thread.sleep(100); // 休眠100ms
System.out.println(Thread.currentThread().getName()+" : isSyncB");
}
}catch (InterruptedException ie) {
}
}
public static synchronized void cSyncA(){
try {
for (int i = 0; i < 5; i++) {
Thread.sleep(100); // 休眠100ms
System.out.println(Thread.currentThread().getName()+" : cSyncA");
}
}catch (InterruptedException ie) {
}
}
public static synchronized void cSyncB(){
try {
for (int i = 0; i < 5; i++) {
Thread.sleep(100); // 休眠100ms
System.out.println(Thread.currentThread().getName()+" : cSyncB");
}
}catch (InterruptedException ie) {
}
}
}
public class LockTest2 {
Something x = new Something();
Something y = new Something();
// 比较(02) x.isSyncA()与y.isSyncA()
private void test2() {
// 新建t21, t21会调用 x.isSyncA()
Thread t21 = new Thread(
new Runnable() {
@Override
public void run() {
x.isSyncA();
}
}, "t21");
// 新建t22, t22会调用 x.isSyncB()
Thread t22 = new Thread(
new Runnable() {
@Override
public void run() {
y.isSyncA();
}
}, "t22");
t21.start(); // 启动t21
t22.start(); // 启动t22
}
public static void main(String[] args) {
LockTest2 demo = new LockTest2();
demo.test2();
}
}
// LockTest3.java的源码
class Something {
public synchronized void isSyncA(){
try {
for (int i = 0; i < 5; i++) {
Thread.sleep(); // 休眠ms
System.out.println(Thread.currentThread().getName()+" : isSyncA");
}
}catch (InterruptedException ie) {
}
}
public synchronized void isSyncB(){
try {
for (int i = 0; i < 5; i++) {
Thread.sleep(100); // 休眠100ms
System.out.println(Thread.currentThread().getName()+" : isSyncB");
}
}catch (InterruptedException ie) {
}
}
public static synchronized void cSyncA(){
try {
for (int i = 0; i < 5; i++) {
Thread.sleep(100); // 休眠100ms
System.out.println(Thread.currentThread().getName()+" : cSyncA");
}
}catch (InterruptedException ie) {
}
}
public static synchronized void cSyncB(){
try {
for (int i = 0; i < 5; i++) {
Thread.sleep(100); // 休眠100ms
System.out.println(Thread.currentThread().getName()+" : cSyncB");
}
}catch (InterruptedException ie) {
}
}
}
public class LockTest3 {
Something x = new Something();
Something y = new Something();
// 比较(03) x.cSyncA()与y.cSyncB()
private void test3() {
// 新建t31, t31会调用 x.isSyncA()
Thread t31 = new Thread(
new Runnable() {
@Override
public void run() {
x.cSyncA();
}
}, "t31");
// 新建t32, t32会调用 x.isSyncB()
Thread t32 = new Thread(
new Runnable() {
@Override
public void run() {
y.cSyncB();
}
}, "t32");
t31.start(); // 启动t31
t32.start(); // 启动t32
}
public static void main(String[] args) {
LockTest3 demo = new LockTest3();
demo.test3();
}
}
// LockTest4.java的源码
class Something {
public synchronized void isSyncA(){
try {
for (int i = 0; i < 5; i++) {
Thread.sleep(100); // 休眠100ms
System.out.println(Thread.currentThread().getName()+" : isSyncA");
}
}catch (InterruptedException ie) {
}
}
public synchronized void isSyncB(){
try {
for (int i = 0; i < 5; i++) {
Thread.sleep(100); // 休眠100ms
System.out.println(Thread.currentThread().getName()+" : isSyncB");
}
}catch (InterruptedException ie) {
}
}
public static synchronized void cSyncA(){
try {
for (int i = 0; i < 5; i++) {
Thread.sleep(100); // 休眠100ms
System.out.println(Thread.currentThread().getName()+" : cSyncA");
}
}catch (InterruptedException ie) {
}
}
public static synchronized void cSyncB(){
try {
for (int i = 0; i < 5; i++) {
Thread.sleep(100); // 休眠100ms
System.out.println(Thread.currentThread().getName()+" : cSyncB");
}
}catch (InterruptedException ie) {
}
}
}
public class LockTest {
Something x = new Something();
Something y = new Something();
// 比较(04) x.isSyncA()与Something.cSyncA()
private void test4() {
// 新建t41, t41会调用 x.isSyncA()
Thread t = new Thread(
new Runnable() {
@Override
public void run() {
x.isSyncA();
}
}, "t41");
// 新建t42, t42会调用 x.isSyncB()
Thread t42 = new Thread(
new Runnable() {
@Override
public void run() {
Something.cSyncA();
}
}, "t42");
t41.start(); // 启动t41
t42.start(); // 启动t42
}
public static void main(String[] args) {
LockTest4 demo = new LockTest4();
demo.test4();
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有