标签:synchronized
public class MyRunnable implements Runnable{
public void run()
{
synchronized (this)
{
try {
for (int i = 0; i <5; i++)
{
Thread.sleep(1000);
System.out.println(Thread.currentThread().getName()+"loop"+i);
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
public class Hello {
public static void main(String []args)
{
MyRunnable run=new MyRunnable();
Thread t1=new Thread(run, "Thread1");
Thread t2=new Thread(run,"Thread2");
t1.start();
t2.start();
}
}
运行结果:public class MyThread extends Thread{ public MyThread(String name) { super(name); } public void run() { synchronized (this) { for(int i=0;i<5;i++) { try { Thread.sleep(1000); System.out.println(Thread.currentThread().getName()+"loop"+i); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } } public class Hello { public static void main(String []args) { MyThread myThread=new MyThread("thread1"); MyThread mm=new MyThread("thread2"); myThread.start(); mm.start(); } }
public class Out {
public void synMethod()//同步方法
{
synchronized (this) {
for(int i=0;i<5;i++)
{
try {
Thread.sleep(1000);
System.out.println(Thread.currentThread().getName()+" loop"+i);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
public void nosynMethod()//非同步方法
{
for(int i=0;i<5;i++)
{
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+" nosynMethod"+i);
}
}
}
public class Hello {
public static void main(String []args)
{
final Out out=new Out();
//t1,t2访问的是同步块,t3访问的时非同步块
Thread t1=new Thread(new Runnable() {
public void run() {
// TODO Auto-generated method stub
out.synMethod();
}
}, "thread1");
Thread t2=new Thread(new Runnable() {
public void run() {
// TODO Auto-generated method stub
out.synMethod();
}
}, "thread2");
Thread t3=new Thread(new Runnable() {
public void run() {
// TODO Auto-generated method stub
out.nosynMethod();
}
}, "thread3");
t1.start();
t2.start();
t3.start();
}
}
public class Out {运行结果分析可得,都是单线程运行的,同一时间内只能使用对象的一个同步方法。
public void synMethod()//同步方法
{
synchronized (this) {
for(int i=0;i<5;i++)
{
try {
Thread.sleep(1000);
System.out.println(Thread.currentThread().getName()+" synMethod"+i);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
public void synMethod2()//非同步方法
{
synchronized (this) {
for(int i=0;i<5;i++)
{
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+" Method"+i);
}
}
}
}
public class Hello {
public static void main(String []args)
{
final Out out=new Out();
//t1,t2访问的是同步块,t3访问的时非同步块
Thread t1=new Thread(new Runnable() {
public void run() {
// TODO Auto-generated method stub
out.synMethod();
}
}, "thread1");
Thread t2=new Thread(new Runnable() {
public void run() {
// TODO Auto-generated method stub
out.synMethod();
}
}, "thread2");
Thread t3=new Thread(new Runnable() {
public void run() {
// TODO Auto-generated method stub
out.synMethod2();
}
}, "thread3");
t1.start();
t2.start();
t3.start();
}
}
public synchronized void foo1()synchronized代码块示例:
{
System.out.println("synchronized methoed");
}
public void foo2()synchronized代码块中的this是指当前对象,也可以将this替换成其他对象。
{
synchronized (this)
{
System.out.println("synchronized methoed");
}
}
版权声明:知识在于分享,技术在于交流,转载时请留一个博主的链接就好
标签:synchronized
原文地址:http://blog.csdn.net/qq924862077/article/details/48096717