当一个程序中有多个线程共同分享同一个资源时,为了避免出现错误就会引入synchronized来限制线程之间的
矛盾。
public class CoreJavaDay21_1 {
public static void main(String[] args) {
SaleTicketRunnable1 saleTicketRunnable = new SaleTicketRunnable1();
Thread t1 = new Thread(saleTicketRunnable, "AAAAA");
Thread t2 = new Thread(saleTicketRunnable, "BBBBB");
t1.start();
t2.start();
}
}
synchronized定义在方法中,那么只有当这个方法结束的时候synchronized才会被释放。
1)public class SaleTicketRunnable3 implements Runnable {
private int ticket = 50;
private boolean flag = true;
public void run() {
while(flag){
saleTicket();
}
}
public synchronized void saleTicket(){
try {
Thread.sleep(300);
} catch (InterruptedException e) {
e.printStackTrace();
}
if(ticket > 0){
ticket--;
System.out.println(Thread.currentThread().getName() + ":" + ticket);
}else{
flag = false;
}
}
}
synchronized定义在while循环中,只有结束本次的循环,synchronized才会被释放。
2)public class SaleTicketRunnable1 implements Runnable{
private int ticket = 500;
private boolean flag = true;
Object obj = new Object();
public void run() {
while(flag){
synchronized (obj) {
try {
Thread.sleep(0);
} catch (InterruptedException e) {
e.printStackTrace();
}
if(ticket > 0){
ticket--;
System.out.println(Thread.currentThread().getName() + ":" + ticket);
}else{
flag = false;
}
}
}
}
}
在2中定义的为object,可以换成this
3)public class SaleTicketRunnable1 implements Runnable{
private int ticket = 500;
private boolean flag = true;
public void run() {
while(flag){
synchronized (this) {
try {
Thread.sleep(0);
} catch (InterruptedException e) {
e.printStackTrace();
}
if(ticket > 0){
ticket--;
System.out.println(Thread.currentThread().getName() + ":" + ticket);
}else{
flag = false;
}
}
}
}
}
需要主要的是,在这三种synchronized的应用中,synchronized都要放在while中,否则会出现一个线程结束这个任务,另一个线程
一直处于就绪状态没有被执行。
矛盾。
public class CoreJavaDay21_1 {
public static void main(String[] args) {
SaleTicketRunnable1 saleTicketRunnable = new SaleTicketRunnable1();
Thread t1 = new Thread(saleTicketRunnable, "AAAAA");
Thread t2 = new Thread(saleTicketRunnable, "BBBBB");
t1.start();
t2.start();
}
}
synchronized定义在方法中,那么只有当这个方法结束的时候synchronized才会被释放。
1)public class SaleTicketRunnable3 implements Runnable {
private int ticket = 50;
private boolean flag = true;
public void run() {
while(flag){
saleTicket();
}
}
public synchronized void saleTicket(){
try {
Thread.sleep(300);
} catch (InterruptedException e) {
e.printStackTrace();
}
if(ticket > 0){
ticket--;
System.out.println(Thread.currentThread().getName() + ":" + ticket);
}else{
flag = false;
}
}
}
synchronized定义在while循环中,只有结束本次的循环,synchronized才会被释放。
2)public class SaleTicketRunnable1 implements Runnable{
private int ticket = 500;
private boolean flag = true;
Object obj = new Object();
public void run() {
while(flag){
synchronized (obj) {
try {
Thread.sleep(0);
} catch (InterruptedException e) {
e.printStackTrace();
}
if(ticket > 0){
ticket--;
System.out.println(Thread.currentThread().getName() + ":" + ticket);
}else{
flag = false;
}
}
}
}
}
在2中定义的为object,可以换成this
3)public class SaleTicketRunnable1 implements Runnable{
private int ticket = 500;
private boolean flag = true;
public void run() {
while(flag){
synchronized (this) {
try {
Thread.sleep(0);
} catch (InterruptedException e) {
e.printStackTrace();
}
if(ticket > 0){
ticket--;
System.out.println(Thread.currentThread().getName() + ":" + ticket);
}else{
flag = false;
}
}
}
}
}
需要主要的是,在这三种synchronized的应用中,synchronized都要放在while中,否则会出现一个线程结束这个任务,另一个线程
一直处于就绪状态没有被执行。