Java deadlock生成需要我们注意的关键点有很多,其实有不少的问题都能在源代码中寻找到不少的答案。下面我们就看看如何才能更好的做出Java deadlock生成。希望大家有所收获。
创新互联是一家以重庆网站建设公司、网页设计、品牌设计、软件运维、seo优化排名、小程序App开发等移动开发为一体互联网公司。已累计为成都除甲醛等众行业中小客户提供优质的互联网建站和软件开发服务。
- public class Deadlock extends Object ...{
- private String objID;
- public Deadlock(String id) ...{
- objID = id;
- }
- public synchronized void checkOther(Deadlock other) ...{
- print("entering checkOther()");
- // simulate some lengthy process
- try ...{
- Thread.sleep(2000);
- } catch (InterruptedException x) ...{
- }
- print("in checkOther() - about to " + "invoke 'other.action()'");
- other.action();
- print("leaving checkOther()");
- }
- public synchronized void action() ...{
- print("entering action()");
- // simulate some work here
- try ...{
- Thread.sleep(500);
- } catch (InterruptedException x) ...{
- }
- print("leaving action()");
- }
- public void print(String msg) ...{
- threadPrint("objID=" + objID + " - " + msg);
- }
- public static void threadPrint(String msg) ...{
- String threadName = Thread.currentThread().getName();
- System.out.println(threadName + ": " + msg);
- }
- public static void main(String[] args) ...{
- final Deadlock obj1 = new Deadlock("obj1");
- final Deadlock obj2 = new Deadlock("obj2");
- Runnable runA = new Runnable() ...{
- public void run() ...{
- obj1.checkOther(obj2);
- }
- };
- Thread threadA = new Thread(runA, "threadA");
- threadA.start();
- try ...{
- Thread.sleep(200);
- } catch (InterruptedException x) ...{
- }
- Runnable runB = new Runnable() ...{
- public void run() ...{
- obj2.checkOther(obj1);
- }
- };
- Thread threadB = new Thread(runB, "threadB");
- threadB.start();
- try ...{
- Thread.sleep(5000);
- } catch (InterruptedException x) ...{
- }
- threadPrint("finished sleeping");
- threadPrint("about to interrupt() threadA");
- threadA.interrupt();
- try ...{
- Thread.sleep(1000);
- } catch (InterruptedException x) ...{
- }
- threadPrint("about to interrupt() threadB");
- threadB.interrupt();
- try ...{
- Thread.sleep(1000);
- } catch (InterruptedException x) ...{
- }
- threadPrint("did that break the deadlock?");
- }
- }
以上就是对Java deadlock生成的详细介绍,希望大家能有所领悟。
【编辑推荐】
文章名称:Javadeadlock生成需要使用者注意源代码
当前路径:http://www.mswzjz.cn/qtweb/news2/241952.html
攀枝花网站建设、攀枝花网站运维推广公司-贝锐智能,是专注品牌与效果的网络营销公司;服务项目有等
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 贝锐智能