使用同步代码块必须要设置一个要锁定的对象,一般为this
//使用同步代码块来实现多线程的同步
class MyThread implements Runnable
{
private int ticket = 10;
@Override
public void run() {
// TODO Auto-generated method stub
for(int i=0;i<1000;i++)
{
//同一时刻,只允许一个线程进入代码块处理
synchronized (this) { //为程序逻辑上锁
if(this.ticket>0) //还有票
{
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+"还有"+this.ticket--+"张票");
}
}
}
}
}
public class Test2
{
public static void main(String[] args) {
MyThread myThread=new MyThread();
Thread thread1=new Thread(myThread,"A");
Thread thread2=new Thread(myThread,"B");
Thread thread3=new Thread(myThread,"C");
thread1.setPriority(Thread.MIN_PRIORITY);
thread2.setPriority(Thread.MAX_PRIORITY);
thread3.setPriority(Thread.MAX_PRIORITY);
thread1.start();
thread2.start();
thread3.start();
}
}
2.同步方法
//同步方法
class MyThread implements Runnable
{
private int ticket = 100;
@Override
public void run() {
// TODO Auto-generated method stub
for(int i = 0;i < 100;i++)
{
this.sale();
}
}
public synchronized void sale()
{
if(this.ticket > 0)
{
try {
Thread.sleep(20);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+"还有"+this.ticket--+"张票");
}
}
}
public class Test2
{
public static void main(String[] args) {
MyThread mThread=new MyThread();
Thread thread1=new Thread(mThread,"A");
Thread thread2=new Thread(mThread,"B");
Thread thread3=new Thread(mThread,"C");
thread1.setPriority(Thread.MIN_PRIORITY);
thread2.setPriority(Thread.MAX_PRIORITY);
thread3.setPriority(Thread.MAX_PRIORITY);
thread1.start();
thread2.start();
thread3.start();
}
}
class Sync
{
public void Test()
{
synchronized (this) {
System.out.println("test方法的开始,当前线程为"+Thread.currentThread().getName());
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("test方法结束,当前线程为"+Thread.currentThread().getName());
}
}
}
class MyThread extends Thread
{
private Sync sync;
public MyThread(Sync sync)
{
this.sync=sync;
}
public void run() {
this.sync.Test();
}
}
public class Test2
{
public static void main(String[] args) {
Sync sync=new Sync();
for(int i=0;i<3;i++)
{
Thread thread=new MyThread(sync);
thread.start();
}
}
}
class Sync
{
public void Test()
{
synchronized (Sync.class) {
System.out.println("test方法的开始,当前线程为"+Thread.currentThread().getName());
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("test方法结束,当前线程为"+Thread.currentThread().getName());
}
}
}
class MyThread extends Thread
{
public void run() {
Sync sync=new Sync();
sync.Test();
}
}
public class Test2
{
public static void main(String[] args) {
for(int i=0;i<3;i++)
{
Thread thread=new MyThread();
thread.start();
}
}
}
static synchronized方法,static方法可以直接类名加方法名调用,方法中无法使用this,所以它锁的不是this,而是类的Class对象,所以,static synchronized方法也相当于全局锁,相当于锁住了代码段。
class MyThread implements Runnable
{
private int ticket = 100;
private Lock ticketLock=new ReentrantLock();
@Override
public void run() {
// TODO Auto-generated method stub
for(int i = 0;i<100;i++)
{
ticketLock.lock();
try {
if (this.ticket > 0)
{ // 还有票
try {
Thread.sleep(20);
}catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + ",还有" + this.ticket-- + " 张票");
}
} finally {
ticketLock.unlock();
}
}
}
}
public class Test2
{
public static void main(String[] args) {
MyThread myThread = new MyThread();
Thread thread1= new Thread(myThread,"A");
Thread thread2= new Thread(myThread,"B");
Thread thread3= new Thread(myThread,"C");
thread1.setPriority(Thread.MIN_PRIORITY);
thread2.setPriority(Thread.MAX_PRIORITY);
thread3.setPriority(Thread.MAX_PRIORITY);
thread1.start();
thread2.start();
thread3.start();
}
}