1,同步代码块
-格式:
synchronized(对象){
需要同步的代码;
}
-注意:这个对象,同步代码块可以解决线程安全问题的根本就在于这个对象。
这个对象就好比是锁的功能。
-这个对象可以是任意对象,但是多个线程必须是同一个对象。
2,同步的好处:
-解决了多线程中的线程安全问题
3,同步的弊端
-当线程很多的时候,因为每个线程来了之后都要判断同步上的锁,这个很
耗费资源和时间,降低了程序的运行效率。
package com.momo.thread;
import com.momo.domain.Stu;
public class MyRunnable implements Runnable{
private int tickets = 100;
// Object o = new Object();
Stu s = new Stu();
@Override
public void run() {
while (true){
// synchronized (new Object()) {//注意不能这样
synchronized (s) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
if (tickets > 0) {
System.out.println(Thread.currentThread().getName() + "卖出了第:" + (tickets--) + "张票");
}
}
}
}
}
package com.momo.demo;
import com.momo.thread.MyRunnable;
public class Demo1 {
public static void main(String[] args) {
MyRunnable mr = new MyRunnable();
Thread t1 = new Thread(mr,"小明");
Thread t2 = new Thread(mr,"小花");
Thread t3 = new Thread(mr,"小美");
t1.start(); t2.start(); t3.start(); }
}
package com.momo.thread;
import com.momo.domain.Stu;
public class MyRunnable implements Runnable{
// private int tickets = 100;
private static int tickets = 100;
// Object o = new Object();
private Stu s = new Stu();
private int i = 0;
@Override public void run() { while (true){ if(i%2==0){ // synchronized (s) { // synchronized (this) { synchronized (MyRunnable.class) { if (tickets > 0) { try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(Thread.currentThread().getName() + "卖出了第:" + (tickets--) + "张票"); } } }else{ /* synchronized (s) { if (tickets > 0) { try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(Thread.currentThread().getName() + "卖出了第:" + (tickets--) + "张票"); } }*/ // maiPiao(); // maiPiao2(); maiPiao3(); } i++; } } /* * 静态同步方法的锁对象是当前类的字节码文件对象 MyRunnable.class * */ private static synchronized void maiPiao3(){ if (tickets > 0) { try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(Thread.currentThread().getName() + "卖出了第:" + (tickets--) + "张票"); } } /* * 同步方法:就是把同步关键字加到方法上 * 锁对象是this * */
/* private synchronized void maiPiao2(){
if (tickets > 0) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + "卖出了第:" + (tickets--) + "张票");
}
}//
* 如果一个方法从刚开始就发现它里面的代码被同步了,那么
* 就可以把这个同步加到方法上, 就变成了同步方法。
* //
synchronized (s) {
if (tickets > 0) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + "卖出了第:" + (tickets--) + "张票");
}
}
}//
public void run() {
while (true){
// synchronized (new Object()) {//注意不能这样
synchronized (s) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
if (tickets > 0) {
System.out.println(Thread.currentThread().getName() + "卖出了第:" + (tickets--) + "张票");
}
}
}
}*/
}
package com.momo.demo;
import com.momo.thread.MyRunnable;
public class Demo1 {
public static void main(String[] args) {
MyRunnable mr = new MyRunnable();
Thread t1 = new Thread(mr,"小明");
Thread t2 = new Thread(mr,"小花");
Thread t3 = new Thread(mr,"小美");
t1.start(); t2.start(); t3.start(); }
}
标签:tickets,MyRunnable,synchronized,Thread,安全,线程,解决,new,100 From: https://blog.51cto.com/u_16230968/7162080