一.synchronized的两个用法
①对象锁
方法锁(sychronized修饰普通方法,默认锁对象为this当前对象)和同步代码块锁(自己指定锁对象)
eg1
public class synchronizedobjectcodeblock2 implements runnable{
static synchronized0bjectcodeblock2 instance = newsynchronized0bjectcodeblock2 ();
@override
public void run() {
synchronized (this) {
System.out.printin(" I am object lock block form. My name is "+
Thread.currentThread ().getName ());
try {
Thread.sleep (millis, 3000);
} catch (InterruptedException e) {e.interruptedexception trace ();}
System.out.println (Thread.currentthread ().getname()+"运行结束");
}}
public static void main(String[] args) {
Thread t1 =new Thread(instance);
Thread t2 =new Thread(instance);
t1 . start ();
t2 . start ();
while (t1. isAlive() ||t2. isAlive()) {
}
system.out.println (finished ");
}}
eg2
public class synchronizedobjectcodeblock2 implements runnable{
static synchronized0bjectcodeblock2 instance = newsynchronized0bjectcodeblock2 ();
Object lock=new Object();
@override
public void run() {
synchronized (lock) {
System.out.printin(" I am object lock block form. My name is "+
Thread.currentThread ().getName ());
try {
Thread.sleep (millis, 3000);
} catch (InterruptedException e) {e.interruptedexception trace ();}
System.out.println (Thread.currentthread ().getname()+"运行结束");
}}
public static void main(String[] args) {
Thread t1 =new Thread(instance);
Thread t2 =new Thread(instance);
t1 . start ();
t2 . start ();
while (t1. isAlive() ||t2. isAlive()) {
}
system.out.println (finished ");
}}
eg3
public class synchronizedobjectcodeblock2 implements runnable{
static synchronized0bjectcodeblock2 instance = newsynchronized0bjectcodeblock2 ();
Object lock1=new Object();
Object lock2=new Object();
@override
public void run() {
synchronized (lock1) {
System.out.printin(" I am object lock1 block form. My name is "+
Thread.currentThread ().getName ());
try {
Thread.sleep (millis, 3000);
} catch (InterruptedException e)
{e.interruptedexception trace ();
}
System.out.println (Thread.currentthread ().getname()+"lock1运行结束");
}
synchronized (lock2) {
System.out.printin(" I am object lock2 block form. My name is "+
Thread.currentThread ().getName ());
try {
Thread.sleep (millis, 3000);
} catch (InterruptedException e) {e.interruptedexception trace ();}
System.out.println (Thread.currentthread ().getname()+"lock2运行结束");
}
}
public static void main(String[] args) {
Thread t1 =new Thread(instance);
Thread t2 =new Thread(instance);
t1 . start ();
t2 . start ();
while (t1. isAlive() ||t2. isAlive()) {
}
system.out.println (finished ");
}}
eg4
public class synchronizedobjectcodeblock2 implements runnable{
static synchronized0bjectcodeblock2 instance = newsynchronized0bjectcodeblock2 ();
Object lock1=new Object();
Object lock2=new Object();
@override
public void run() {
synchronized (lock1) {
System.out.printin(" I am object lock1 block form. My name is "+
Thread.currentThread ().getName ());
try {
Thread.sleep (millis, 3000);
} catch (InterruptedException e)
{e.interruptedexception trace ();
}
System.out.println (Thread.currentthread ().getname()+"lock1运行结束");
}
synchronized (lock1) {
System.out.printin(" I am object lock2 block form. My name is "+
Thread.currentThread ().getName ());
try {
Thread.sleep (millis, 3000);
} catch (InterruptedException e) {e.interruptedexception trace ();}
System.out.println (Thread.currentthread ().getname()+"lock2运行结束");
}
}
public static void main(String[] args) {
Thread t1 =new Thread(instance);
Thread t2 =new Thread(instance);
t1 . start ();
t2 . start ();
while (t1. isAlive() ||t2. isAlive()) {
}
system.out.println (finished ");
}}
eg5
public class synchronizedobjectcodeblock3 implements runnable{
static synchronized0bjectcodeblock3 instance = newsynchronized0bjectcodeb