码迷,mamicode.com
首页 > 其他好文 > 详细

Lombok(1.14.8) - @Synchronized

时间:2015-08-02 00:48:29      阅读:219      评论:0      收藏:0      [点我收藏+]

标签:

@Synchronized

@Synchronized,实现同步。

package com.huey.lombok;

import java.util.Date;

import lombok.Synchronized;

public class SynchronizedExample implements Runnable {

    @Override
    public void run() {
        sayHello();
    }

    @Synchronized
    public void sayHello() {
        System.out.println("hello, " + Thread.currentThread().getName() + "! Now is " + new Date());
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        SynchronizedExample foo = new SynchronizedExample();
        new Thread(foo).start();    // [OUTPUT]: hello, Thread-0! Now is Sat Aug 01 10:55:08 CST 2015
        new Thread(foo).start();    // [OUTPUT]: hello, Thread-1! Now is Sat Aug 01 10:55:11 CST 2015
    }
}

 

Lombok(1.14.8) - @Synchronized

标签:

原文地址:http://www.cnblogs.com/huey/p/4693752.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!