/*
* Copyright © Yolanda. All Rights Reserved
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.yolanda.multithread;
import java.io.Serializable;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
/**
* Created in Mar 27, 2016 11:33:04 AM.
*
* @author Yolanda;
*/
public class Tasker implements Serializable, Comparable<Tasker> {
private static final long serialVersionUID = 1565L;
private static Integer value = 0;
/** Lock模块事例 **/
private Lock lock;
public void change2() {
lock.lock();
try {
value++;
System.out.println(value);
if (value == 50)
throw new RuntimeException("");
} finally {
lock.unlock();
}
}
/** synchronized模块事例 **/
private static Object object = new Object();
/**
* 执行任务;synchronized锁定方法体。
*/
public void change() {
synchronized (object) {
value++;
System.out.println(value);
}
if (value == 50)
throw new RuntimeException("");
}
/**
* 执行任务;synchronized锁定方法。
*/
// public synchronized void change() {
// value++;
// System.out.println(value);
// }
@Override
public int compareTo(Tasker o) {
return 0;
}
private Tasker() {
lock = new ReentrantLock();
}
private static Tasker tasker;
public synchronized static Tasker getInstance() {
if (tasker == null)
tasker = new Tasker();
return tasker;
}
}
Java、Android多线程、线程安全、线程同步
需积分: 50 62 浏览量
2016-03-27
16:59:55
上传
评论 4
收藏 9KB ZIP 举报
严振杰
- 粉丝: 4789
- 资源: 60
最新资源
- 基于yolov5识别算法实现的DNF自动脚本源码.zip
- 基于Python实现的自动化办公项目.zip
- 基于python实现的基于PyQt5和爬虫的小说阅读系统.zip
- 机械设计整经机上纱自动化sw20非常好的设计图纸100%好用.zip
- Screenshot_20240427_031602.jpg
- 网页PDF_2024年04月26日 23-46-14_QQ浏览器网页保存_QQ浏览器转格式(6).docx
- 直接插入排序,冒泡排序,直接选择排序.zip
- 在排序2的基础上,再次对快排进行优化,其次增加快排非递归,归并排序,归并排序非递归版.zip
- 实现了7种排序算法.三种复杂度排序.三种nlogn复杂度排序(堆排序,归并排序,快速排序)一种线性复杂度的排序.zip
- 冒泡排序 直接选择排序 直接插入排序 随机快速排序 归并排序 堆排序.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈