面试题目——《CC150》线程与锁
package cc150.thread_lock; public class RunnableThreadExample implements Runnable{ public int count = 0; public static void main(String[] args) { // TODO 自动生成的方法存根 RunnableThreadExample instance = new RunnableThreadExample(); Thread thread = new Thread(instance); thread.start(); //等到上面的线程数到5 while(instance.count != 5){ try{ Thread.sleep(250); System.out.println("等待"); }catch(InterruptedException exc){ exc.printStackTrace(); } } } @Override public void run() { // TODO 自动生成的方法存根 System.out.println("RunnableThread开始"); try{ while(count < 5){ Thread.sleep(500); count++; } }catch(InterruptedException exc){ System.out.println("RunnableThread中断"); } System.out.println("RunnableThread终止"); } }
package cc150.thread_lock; public class ThreadExample{ public static void main(String[] args){ // TODO 自动生成的方法存根 ThreadExample th = new ThreadExample(); threadExample instance = th.new threadExample(); instance.start(); //等到上面的线程数到5 while(instance.count != 5){ try{ Thread.sleep(250); System.out.println("等待"); }catch(InterruptedException exc){ exc.printStackTrace(); } } } public class threadExample extends Thread{ int count = 0; public void run() { // TODO 自动生成的方法存根 System.out.println("Thread开始"); try{ while(count < 5){ Thread.sleep(500); System.out.println("在线程中,count是"+count); count++; } }catch(InterruptedException exc){ System.out.println("RunnableThread中断"); } System.out.println("RunnableThread终止"); } } }
package cc150.thread_lock; class MyObject{ public synchronized void foo(String name){ //加上synchronized关键字,给foo提供同步 try{ System.out.println("线程"+name+".foo()开始"); Thread.sleep(3000); System.out.println("线程"+name+".foo()结束"); }catch(InterruptedException exc){ System.out.println("线程"+name+"中断"); } } } public class MyClass extends Thread{ //不同的线程,来调用上面的foo private String name; private MyObject myObj; public MyClass(MyObject obj,String n){ name = n; myObj = obj; } public void run(){ myObj.foo(name); } public static void main(String[] args) { // TODO 自动生成的方法存根 // MyObject obj1 = new MyObject(); // MyObject obj2 = new MyObject(); // MyClass thread1 = new MyClass(obj1,"1"); // MyClass thread2 = new MyClass(obj2,"2"); // thread1.start(); // thread2.start(); //相同的obj引用,只能一个线程可以调用foo,另一个线程必须等待 MyObject obj = new MyObject(); MyClass thread11 = new MyClass(obj,"1"); MyClass thread22 = new MyClass(obj,"2"); thread11.start(); thread22.start(); } }
package cc150.thread_lock; class MyObject{ public static synchronized void foo(String name){ //加上synchronized关键字,给foo提供同步 try{ System.out.println("线程"+name+".foo()开始"); Thread.sleep(3000); System.out.println("线程"+name+".foo()结束"); }catch(InterruptedException exc){ System.out.println("线程"+name+"中断"); } } public static synchronized void bar(String name){ //加上synchronized关键字,给bar提供同步 try{ System.out.println("线程"+name+".bar()开始"); Thread.sleep(3000); System.out.println("线程"+name+".bar()结束"); }catch(InterruptedException exc){ System.out.println("线程"+name+"中断"); } } } public class MyClass extends Thread{ //不同的线程,来调用上面的foo private String name; private MyObject myObj; public MyClass(MyObject obj,String n){ name = n; myObj = obj; } public void run(){ //myObj.foo(name); if(name.equals("1")) MyObject.foo(name); else if(name.equals("2")) MyObject.bar(name); } public static void main(String[] args) { // TODO 自动生成的方法存根 // MyObject obj1 = new MyObject(); // MyObject obj2 = new MyObject(); // MyClass thread1 = new MyClass(obj1,"1"); // MyClass thread2 = new MyClass(obj2,"2"); // thread1.start(); // thread2.start(); //相同的obj引用,只能一个线程可以调用foo,另一个线程必须等待 MyObject obj = new MyObject(); MyClass thread11 = new MyClass(obj,"1"); MyClass thread22 = new MyClass(obj,"2"); thread11.start(); thread22.start(); } }
package cc150.thread_lock; class MyObject2{ public synchronized void foo(String name){ //加上synchronized块,给foo提供同步 synchronized(this){ try{ System.out.println("线程"+name+".foo()开始"); Thread.sleep(3000); System.out.println("线程"+name+".foo()结束"); }catch(InterruptedException exc){ System.out.println("线程"+name+"中断"); } } } } public class MyClass2 extends Thread{ //不同的线程,来调用上面的foo private String name; private MyObject2 myObj; public MyClass2(MyObject2 obj,String n){ name = n; myObj = obj; } public void run(){ myObj.foo(name); } public static void main(String[] args) { // TODO 自动生成的方法存根 // MyObject obj1 = new MyObject(); // MyObject obj2 = new MyObject(); // MyClass thread1 = new MyClass(obj1,"1"); // MyClass thread2 = new MyClass(obj2,"2"); // thread1.start(); // thread2.start(); //相同的obj引用,只能一个线程可以调用foo,另一个线程必须等待 MyObject2 obj = new MyObject2(); MyClass2 thread11 = new MyClass2(obj,"1"); MyClass2 thread22 = new MyClass2(obj,"2"); thread11.start(); thread22.start(); } }
package cc150.thread_lock; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; class MyObj { private Lock lock; private int balance = 100; //剩余 public MyObj(){ //构造函数 lock = new ReentrantLock(); //可重入锁 } public int withdraw(int value){ //取款 lock.lock(); int temp = balance; try{ Thread.sleep(100); temp = temp-value; Thread.sleep(100); balance = temp; }catch(InterruptedException e){ } lock.unlock(); return temp; } public int deposit(int value){ //存款 lock.lock(); int temp = balance; try{ Thread.sleep(100); temp = temp+value; Thread.sleep(100); balance = temp; }catch(InterruptedException e){ } lock.unlock(); return temp; } } public class LockedATM extends Thread{ private int value; private MyObj myObj; private String str; public LockedATM(MyObj obj,String s,int v){ value = v; myObj = obj; str = s; } public void run(){ if(str.equals("withdraw")) System.out.println(myObj.withdraw(value)); else if(str.equals("deposit")) System.out.println(myObj.deposit(value)); } public static void main(String[] args) { // TODO 自动生成的方法存根 MyObj obj1 = new MyObj(); MyObj obj2 = new MyObj(); LockedATM thread1 = new LockedATM(obj1,"withdraw",50); //两个线程执行的顺序不一定 LockedATM thread2 = new LockedATM(obj2,"deposit",50); //可能先存款,也可能先取款 thread1.start(); thread2.start(); } }
本文只发表于博客园和tonglin0325的博客,作者:tonglin0325,转载请注明原文链接:https://www.cnblogs.com/tonglin0325/p/5959613.html