MENU

Keep It Stupid Simple

Java并发基础

Synchronized

synchronized关键字是用来控制线程同步的;可以加在代码块上,也可以加在方法上。

不使用synchronized

public class SynchronizedDemo {
    private int count = 10;
    public void m() {
        count--;
        System.out.println(Thread.currentThread().getName() + "->" + count);
    }

    public static void main(String[] args) {
        SynchronizedDemo sync = new SynchronizedDemo();
        for (int i = 0; i < 10; i++) {
            Thread thread = new Thread(sync::m, "thread" + i);
            thread.start();
        }
    }
}

执行结果:

thread0->8
thread2->7
thread1->8
thread3->6
thread4->5
thread5->4
thread6->3
thread7->2
thread8->1
thread9->0

synchronized语法

任何线程要执行代码块的代码,必须先拿到obj的锁:

public class SynchronizedDemo {
    private int count = 10;
    private final Object obj = new Object();

    public void m() {
        synchronized (obj) {
            count--;
            System.out.println(count);
        }
    }
}

Read More

Linux 上古神器之 sed

1.sed介绍

sed是stream editor(流编辑器)的简称,出生于1973年;不同于vim这类交互式编辑器,它没有编辑界面、快捷键和光标,只通过一次输入的编辑指令来处理输入流并输出结果,因此效率更高;另外,sed还能够过滤文本输出选定行。

sed一次处理一行内容,存储到一个临时缓冲区,即“模式空间”(pattern space),处理完成后输出到标注出,然后sed再获取下一行内容到模式空间处理,直到全部处理完成。

Read More

常用 LaTex 数学符号表

Greek Letters

符号命令符号命令符号命令符号命令
\(\alpha\) \alpha\(\beta\) \beta\(\gamma\) \gamma\(\delta\) \delta
\(\epsilon\) \epsilon\(\zeta\) \zeta\(\eta\) \eta\(\theta\) \theta
\(\iota\) \iota\(\kappa\) \kappa\(\lambda\) \lambda\(\mu\) \mu
\(\nu\) \nu\(\xi\) \xi\(\omicron\) \omicron\(\pi\) \pi
\(\rho\) \rho\(\sigma\) \sigma\(\tau\) \tau\(\upsilon\) \upsilon
\(\phi\) \phi\(\chi\) \chi\(\psi\) \psi\(\omega\) \omega
\(\Gamma\) \Gamma\(\Lambda\) \Lambda\(\Sigma\) \Sigma\(\Psi\) \Psi
\(\Delta\) \Delta\(\Xi\) \Xi\(\Upsilon\) \Upsilon\(\Omega\) \Omega
\(\Theta\) \Theta\(\Pi\) \Pi\(\Phi\) \Phi

Binary Operation Symbols

符号命令符号命令符号命令符号命令
\(\pm\) \pm\(\cap\) \cap\(\diamond\) \diamond\(\oplus\) \oplus
\(\mp\) \mp\(\cup\) \cup\(\bigtriangleup\) \bigtriangleup\(\ominus\) \ominus
\(\times\) \times\(\uplus\) \uplus\(\bigtriangledown\) \bigtriangledown\(\otimes\) \otimes
\(\div\) \div\(\sqcap\) \sqcap\(\triangleleft\) \triangleleft\(\oslash\) \oslash
\(\ast\) \ast\(\sqcup\) \sqcup\(\triangleright\) \triangleright\(\odot\) \odot
\(\star\) \star\(\vee\) \vee\(\+\) \+\(\bigcirc\) \bigcirc
\(\circ\) \circ\(\wedge\) \wedge\(\-\) \-\(\dagger\) \dagger
\(\bullet\) \bullet\(\setminus\) \setminus\(\amalg\) \amalg\(\ddagger\) \ddagger
\(\cdot\) \cdot\(\wr\) \wr

Read More