zoukankan      html  css  js  c++  java
  • 多线程,同步代码块。

    public class Demo07 {
    public static void main(String[] args) {
    final Printer p = new Printer();
    new Thread() {
    public void run() {
    while (true) {
    p.print1();
    }
    }
    }.start();
    }

    static class Printer {
    Demo01 d = new Demo01();

    public void print1() {
    synchronized (d) { //锁对象不能用匿名对象,因为匿名对象不是同一个对象;同步代码块 ,锁机制,锁对象可以是任意的对象。
    System.out.print("黑");
    System.out.print("马");
    System.out.print("程");
    System.out.print("序");
    System.out.print("员");
    System.out.print(" ");
    }
    }

    public void print2() {
    synchronized (d) {
    System.out.print("传");
    System.out.print("智");
    System.out.print("播");
    System.out.print("客");
    System.out.print(" ");
    }
    }

    }
    }
  • 相关阅读:
    MongoDB 查询$关键字 $in $or $all
    MongoDB limit 选取 skip跳过 sort排序 方法
    POJ 1700
    POJ 1666
    POJ 1701
    POJ 1674
    POJ 1664
    POJ 1665
    POJ 1658
    POJ 1656
  • 原文地址:https://www.cnblogs.com/wangffeng293/p/13363289.html
Copyright © 2011-2022 走看看