0037 Java學習筆記-多線程-同步代碼塊、同步辦法、同步鎖。本站提示廣大學習愛好者:(0037 Java學習筆記-多線程-同步代碼塊、同步辦法、同步鎖)文章只能為提供參考,不一定能成為您想要的結果。以下是0037 Java學習筆記-多線程-同步代碼塊、同步辦法、同步鎖正文
package testpack;
public class Test1 {
public static void main(String[] args){
System.out.println("如今是主線程: "+Thread.currentThread());
System.out.println("上面新建兩個線程");
A a=new A(500);
new Thread(a,"線程A").start();
new Thread(a,"線程B").start();
new Thread(a,"線程C").start();
}
}
class A implements Runnable{
private int tickets;
A (int tick){
tickets=tick;
}
private Object obj=new Object(); //同步監視器
public void run() {
synchronized(obj){ //同步代碼塊
for (;tickets>0;tickets--) {
System.out.println("以後線程:"+Thread.currentThread()+" 賣出第 "+tickets+" 張票。");
try{
Thread.sleep(1); //讓以後線程暫停1毫秒,其他線程也不能執行該同步代碼塊
}catch(InterruptedException ex){
ex.printStackTrace();
}
if (tickets==1){
System.out.println("票已賣完,以後線程是: "+Thread.currentThread());
}
}
}
}
}
package testpack;
public class Test1 {
public static void main(String[] args){
System.out.println("如今是主線程: "+Thread.currentThread());
System.out.println("上面新建兩個線程");
A a=new A(500);
new Thread(a,"線程A").start();
new Thread(a,"線程B").start();
new Thread(a,"線程C").start();
}
}
class A implements Runnable{
private int tickets;
A (int tick){
tickets=tick;
}
public synchronized void run() { //同步辦法
for (;tickets>0;tickets--) {
System.out.println("以後線程:"+Thread.currentThread()+" 賣出第 "+tickets+" 張票。");
try{
Thread.sleep(1);
}catch(InterruptedException ex){
ex.printStackTrace();
}
if (tickets==1){
System.out.println("票已賣完,以後線程是: "+Thread.currentThread());
}
}
}
}
package testpack;
import java.util.concurrent.locks.ReentrantLock;
public class Test1 {
public static void main(String[] args){
System.out.println("如今是主線程: "+Thread.currentThread());
System.out.println("上面新建兩個線程");
A a=new A(50);
new Thread(a,"線程A").start();
new Thread(a,"線程B").start();
new Thread(a,"線程C").start();
}
}
class A implements Runnable{
private final ReentrantLock lock=new ReentrantLock(); //定義一個同步鎖
private int tickets;
A (int tick){
tickets=tick;
}
public void run() {
lock.lock(); //加鎖
for (;tickets>0;tickets--) {
System.out.println("以後線程:"+Thread.currentThread()+" 賣出第 "+tickets+" 張票。");
if (tickets==1){
System.out.println("票已賣完,以後線程是: "+Thread.currentThread());
}
}
lock.unlock(); //釋放鎖
}
}
package testpack;
public class Test1 {
public static void main(String[] args){
DeadLock dl=new DeadLock();
new Thread(dl).start();
dl.init();
}
}
class DeadLock implements Runnable {
A a=new A();
B b=new B();
public void init(){
a.a1(b);
System.out.println("進入主線程");
}
public void run(){
b.b1(a);
System.out.println("進入子線程");
}
}
class A {
public synchronized void a1(B b){
System.out.println("以後線程是:"+Thread.currentThread().getName()+" ,正在執行a1()");
try{
Thread.sleep(10);
}catch(InterruptedException ex){
ex.printStackTrace();
}
System.out.println("以後線程是:"+Thread.currentThread().getName()+" ,預備調用b2()");
b.b2(); //b2辦法是同步辦法,調用該辦法要對調用的對象b加鎖
}
public synchronized void a2(){
System.out.println("這是a2()辦法");
}
}
class B{
public synchronized void b1(A a){
System.out.println("以後線程是:"+Thread.currentThread().getName()+" ,正在執行b1()");
try{
Thread.sleep(10);
}catch(InterruptedException ex){
ex.printStackTrace();
}
System.out.println("以後線程是:"+Thread.currentThread().getName()+" ,預備調用a2()");
a.a2(); //a2辦法是同步辦法,調用該辦法要對調用的對象a加鎖
}
public synchronized void b2(){
System.out.println("這是b2()辦法");
}
}