package 对象及变量的并发访问2;
import com.sun.xml.internal.ws.api.model.wsdl.WSDLOutput;
import javax.rmi.ssl.SslRMIClientSocketFactory;
/**
* 静态同步synchronized方法与synchronized(class)代码块。
*
* 关键字synchronize对还可以应用在static静态方法上,是对当前的*java文件所对应的
*Class类对象进行持锁,Class类的对象是单例的,更具体地说,在静态static方法上使用
*synchronized关键字声明同步方法时。使用当前静态方法所在类对应Class类的单例对象作为锁。
*
* 从结果看:将synchronized关键字加到非static放上的效果是一样的—---同步的
* 但是,两者还是有本质的区别的,synchronized关键字加到static静态方法上的方式是将
* Class类对象作为锁,而synchronized关键字加到非static静态方法上是是将该方法所在的类的对象作为锁。
*
* 从第二个结果看:异步运行的原因是持有不同的锁,一个是将Service2T2214作为对象的锁,另一个是将Service
* 类对应Class类的对象作为锁,A,B线程和C线程是异步的关系,而A和B是同步的。
*/
class ServiceT2214{
public synchronized static void printA(){
try {
System.out.println("线程名="+Thread.currentThread().getName()+"进入printA时间"+System.currentTimeMillis());
Thread.sleep(3000);
System.out.println("线程名="+Thread.currentThread().getName()+"进入printa时间"+System.currentTimeMillis());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
synchronized public static void pringB(){
System.out.println("线程名="+Thread.currentThread().getName()+
"进入printB的时间"+System.currentTimeMillis());
System.out.println("线程名="+Thread.currentThread().getName()+
"进入printB的时间"+System.currentTimeMillis());
}
}
class Service2T2214{
synchronized public static void printA(){
try {
System.out.println("线程名为:"+Thread.currentThread().getName()+
"在"+System.currentTimeMillis()+"进入printA函数");
Thread.sleep(3000);
System.out.println("线程名为:"+Thread.currentThread().getName()+
"在"+ System.currentTimeMillis()+"离开printA函数");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
synchronized public static void printB(){
System.out.println("线程名为:"+Thread.currentThread().getName()+
"在"+System.currentTimeMillis()+"进入printB函数");
System.out.println("线程名为:"+Thread.currentThread().getName()+
"在"+System.currentTimeMillis()+"离开printB函数");
}
synchronized public void printC(){
System.out.println("线程名为:"+Thread.currentThread().getName()+
"在"+System.currentTimeMillis()+"进入printC函数");
System.out.println("线程名为:"+Thread.currentThread().getName()+
"在"+System.currentTimeMillis()+"离开printC函数");
}
}
class MyThreadAT2214 extends Thread{
@Override
public void run() {
ServiceT2214.printA();
}
}
class MyThreadA2T2214 extends Thread{
private Service2T2214 service2T2214;
public MyThreadA2T2214 (Service2T2214 service2T2214){
super();
this.service2T2214=service2T2214;
}
@Override
public void run() {
Service2T2214.printA();
}
}
class MyThreadBT2214 extends Thread{
@Override
public void run() {
ServiceT2214.pringB();
}
}
class MyThreadB2T2214 extends Thread{
private Service2T2214 service2T2214;
public MyThreadB2T2214(Service2T2214 service2T2214){
super();
this.service2T2214=service2T2214;
}
@Override
public void run() {
Service2T2214.printB();
}
}
class MyThreadCT2214 extends Thread{
private Service2T2214 service2T2214;
public MyThreadCT2214(Service2T2214 service2T2214){
super();
this.service2T2214=service2T2214;
}
@Override
public void run() {
service2T2214.printC();
}
}
class RunT2214{
public RunT2214(){
MyThreadAT2214 at2214=new MyThreadAT2214();
at2214.setName("A");
at2214.start();
MyThreadBT2214 bt2214=new MyThreadBT2214();
bt2214.setName("B");
bt2214.start();
}
}
class Run2T2214{
public Run2T2214(){
Service2T2214 service2T2214=new Service2T2214();
MyThreadA2T2214 a2T2214=new MyThreadA2T2214(service2T2214);
a2T2214.setName("A");
a2T2214.start();
MyThreadB2T2214 b2T2214=new MyThreadB2T2214(service2T2214);
b2T2214.setName("B");
b2T2214.start();
MyThreadCT2214 ct2214=new MyThreadCT2214(service2T2214);
ct2214.setName("C");
ct2214.start();
}
}
public class T2214 {
public static void main(String[] args) {
//RunT2214 runT2214=new RunT2214();
Run2T2214 run2T2214=new Run2T2214();
}
}
RunT2214 runT2214=new RunT2214();
Run2T2214 run2T2214=new Run2T2214();