标签:资源 机制 alt start 创建 new t oid ati java
synchronized(同步监视器){
需要被同步的代码
}
举例说明
class Thread implements Runnable{
private Object obj = new Object();
public void run() {
//使用类对象充当锁
synchronized(obj){
.......
}
}
}
/**
* 创建三个窗口买票,票数100张:使用实现Runnable接口的方式实现的
*/
class WindowThread implements Runnable{
private int ticket = 100;
// private Object obj = new Object();
public void run() {
while (true) {
//此时this:唯一的WindowThread对象
synchronized(this){// 方式二:synchronized(obj){
if (ticket > 0) {
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread(). getName() + ":" + "买票,票号为" + ticket);
ticket--;
}else{
break;
}
}
}
}
}
public class Test1 {
public static void main(String[] args) {
WindowThread window = new WindowThread();
Thread w1 = new Thread(window);
Thread w2 = new Thread(window);
Thread w3 = new Thread(window);
w1.setName("窗口1");
w1.start();
w2.setName("窗口2");
w2.start();
w3.setName("窗口3");
w3.start();
}
}
class Window extends Thread {
// 大家公用数据,只有100张票
private static int ticket = 100;
private static Object obj = new Object();
public void run() {
while (true) {
//方式二
synchronized(Window.class){
// 方式一:synchronized(obj){
//synchronized(this)错误的,此时this代表着三个对象
if(ticket > 0){
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(getName() + ":" + "买票,票号为" + ticket);
ticket--;
}else{
break;
}
}
}
}
}
public class Test2 {
public static void main(String[] args) {
Window w1 = new Window();
Window w2 = new Window();
Window w3 = new Window();
w1.setName("窗口1");
w2.setName("窗口2");
w3.setName("窗口3");
w1.start();
w2.start();
w3.start();
}
}
如果操作共享数据的代码完整的声明在一个方法中,就可以将此方法声明同步的
利用synchronized 修饰方法
public synchronized void XXX(){
}
或
public static synchronized void XXX(){
}
非静态同步方法,调用this
class WindowThread3 implements Runnable{
private int ticket = 100;
private static boolean isFlag = true;
// private Object obj = new Object();
public void run() {
while (isFlag) {
show();
}
}
public synchronized void show(){//同步监视器:this
if (ticket > 0) {
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread(). getName() + ":" + "买票,票号为" + ticket);
ticket--;
}else{
isFlag = false;
}
}
}
public class Test3 {
public static void main(String[] args) {
WindowThread3 window = new WindowThread3();
Thread w1 = new Thread(window);
Thread w2 = new Thread(window);
Thread w3 = new Thread(window);
w1.setName("窗口1");
w1.start();
w2.setName("窗口2");
w2.start();
w3.setName("窗口3");
w3.start();
}
}
静态同步方法,调用当前类本身
class Window4 extends Thread{
private static int ticket = 100;
private static boolean isFlag = true;
@Override
public void run() {
while(isFlag){
show();
}
}
public static synchronized void show(){
//同步监视器:Window.class
if(ticket > 0){
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread(). getName() + ":" + "买票,票号为" + ticket);
ticket--;
}else{
isFlag = false;
}
}
}
public class Test4 {
public static void main(String[] args) {
Window4 w1 = new Window4();
Window4 w2 = new Window4();
Window4 w3 = new Window4();
w1.setName("窗口1");
w2.setName("窗口2");
w3.setName("窗口3");
w1.start();
w2.start();
w3.start();
}
}
实例化ReentrantLock
private ReentrantLock lock = new ReentrantLock(true);
调用锁的方法
lock.lock();
调用解锁的方法
lock.unlock();
注意:其中调用lock()方法和unlock()方法时要用try()finally()包住
class Window5 implements Runnable {
private int ticket = 100;
//1.实例化ReentrantLock
private ReentrantLock lock = new ReentrantLock(true);
public void run() {
while (true) {
try{
//2.调用锁定的方法:lock()
lock.lock();
if (ticket > 0) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + "卖票" + ":" + "票号为" + ticket);
ticket--;
}else{
break;
}
}finally{
//3.调用解锁的方法:unlock()
lock.unlock();
}
}
}
}
public class Test5 {
public static void main(String[] args) {
Window5 window = new Window5();
Thread t1 = new Thread(window);
Thread t2 = new Thread(window);
Thread t3 = new Thread(window);
t1.setName("窗口1:");
t2.setName("窗口2:");
t3.setName("窗口3:");
t1.start();
t2.start();
t3.start();
}
}
Lock--->同步代码块--->同步方法
标签:资源 机制 alt start 创建 new t oid ati java
原文地址:https://www.cnblogs.com/CrabDumplings/p/13357235.html