Java
同步:多个线程访问同一份资源 确保资源安全-->线程安全
synchronized 同步
一 同步块
synchronized(引用类型|this|类.class){
}
例如:
public class TestSynchronized {
public static void main(String[] args){
Food food=new Food();
Thread T1=new Thread(food,"吃货");
Thread T2=new Thread(food,"饿货");
Thread T3=new Thread(food,"美食家");
T1.start();
T2.start();
T3.start();
}
}
class Food implements Runnable{
private int num=20;
private boolean flag=true;
@Override
public void run() {
while(flag){
getFood();
}
}
public void getFood(){
synchronized (this){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
if(num<=0){
flag=false;
return;
}
System.out.println(Thread.currentThread().getName()+"抢到了第 "+num--+" 个包子");
}
}
}
二 同步方法 在方法前加synchronized
例如:
public class TestSynchronized {
public static void main(String[] args){
Food food=new Food();
Thread T1=new Thread(food,"吃货");
Thread T2=new Thread(food,"饿货");
Thread T3=new Thread(food,"美食家");
T1.start();
T2.start();
T3.start();
}
}
class Food implements Runnable{
private int num=20;
private boolean flag=true;
@Override
public void run() {
while(flag){
getFood();
}
}
public synchronized void getFood(){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
if(num<=0){
flag=false;
return;
}
System.out.println(Thread.currentThread().getName()+"抢到了第 "+num--+" 个包子");
}
}
运用synchronized来解决多线程中的单例设计模式失败问题
单例设计模式:
懒汉式:
1 构造方法私有化,避免外部直接创建对象
2 声明一个私有的静态变量
3 创建一个对外的公共静态方法,访问该变量,如果变量没有对象,创建该对象
/*
*懒汉式
*/
public class Singletance1 {
public static void main(String[] args){
Singletance1 s1=Singletance1.getSingletance();
Singletance1 s2=Singletance1.getSingletance();
System.out.println(s1.hashCode()+" "+s2.hashCode());
}
private Singletance1(){
}
private static Singletance1 instance=null;
public static synchronized Singletance1 getSingletance(){
if(instance==null){
instance=new Singletance1();
}
return instance;
}
public static Singletance1 getSingletance1(){
synchronized (Singletance1.class){
if(null==instance){
instance=new Singletance1();
}
return instance;
}
}
public static Singletance1 getSingletance2(){
if(null==instance){
synchronized (Singletance1.class){
if(null==instance)
instance=new Singletance1();
}
}
return instance;
}
}
饿汉式:
/*
*创建并初始化一个静态私有对象,构造函数私有化
*,提供一个对外的静态共有方法用于获取该实例对象
*/
public class Singletance2 {
private static Singletance2 instance=new Singletance2();
private Singletance2(){
}
public static Singletance2 getInstance(){
return instance;
}
}
生产者消费者模式:使用管程或信号灯来解决死锁问题
package TestThread;
/**
* Created by lenovo on 2016/1/22.
*/
public class Movie {
/**
* flag=true 生产者生产,消费者等待,生产完成后通知消费者消费
* flag=false 消费者消费,生产者等待,消费消费后通知生产者生产
*/
private String pic;
private boolean flag=true;
、
public synchronized void play(String pic){
if(!flag){
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("播放了"+pic);
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
this.pic=pic;
this.notify();
this.flag=false;
}
public synchronized void watch(){
if(flag){
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("观看了"+this.pic);
this.notifyAll();
this.flag=true;
}
}
package TestThread;
/**
* Created by lenovo on 2016/1/22.
*/
public class Player implements Runnable{
private Movie movie;
public Player(Movie movie) {
this.movie = movie;
}
@Override
public void run() {
for(int i=0;i<20;i++){
if(i%2==0){
movie.play("十面埋伏");
}else{
movie.play("霸王别姬");
}
}
}
}
package TestThread;
/**
* Created by lenovo on 2016/1/22.
*/
public class Watcher implements Runnable{
private Movie movie;
public Watcher(Movie movie) {
this.movie = movie;
}
@Override
public void run() {
for(int i=0;i<20;i++){
movie.watch();
}
}
}
package TestThread;
/**
* Created by lenovo on 2016/1/22.
*/
public class Cinema {
public static void main(String[] args){
Movie movie=new Movie();
Player player=new Player(movie);
Watcher watcher=new Watcher(movie);
new Thread(player).start();
new Thread(watcher).start();
}
同步:多个线程访问同一份资源 确保资源安全-->线程安全
synchronized 同步
一 同步块
synchronized(引用类型|this|类.class){
}
例如:
public class TestSynchronized {
public static void main(String[] args){
Food food=new Food();
Thread T1=new Thread(food,"吃货");
Thread T2=new Thread(food,"饿货");
Thread T3=new Thread(food,"美食家");
T1.start();
T2.start();
T3.start();
}
}
class Food implements Runnable{
private int num=20;
private boolean flag=true;
@Override
public void run() {
while(flag){
getFood();
}
}
public void getFood(){
synchronized (this){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
if(num<=0){
flag=false;
return;
}
System.out.println(Thread.currentThread().getName()+"抢到了第 "+num--+" 个包子");
}
}
}
二 同步方法 在方法前加synchronized
例如:
public class TestSynchronized {
public static void main(String[] args){
Food food=new Food();
Thread T1=new Thread(food,"吃货");
Thread T2=new Thread(food,"饿货");
Thread T3=new Thread(food,"美食家");
T1.start();
T2.start();
T3.start();
}
}
class Food implements Runnable{
private int num=20;
private boolean flag=true;
@Override
public void run() {
while(flag){
getFood();
}
}
public synchronized void getFood(){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
if(num<=0){
flag=false;
return;
}
System.out.println(Thread.currentThread().getName()+"抢到了第 "+num--+" 个包子");
}
}
运用synchronized来解决多线程中的单例设计模式失败问题
单例设计模式:
懒汉式:
1 构造方法私有化,避免外部直接创建对象
2 声明一个私有的静态变量
3 创建一个对外的公共静态方法,访问该变量,如果变量没有对象,创建该对象
/*
*懒汉式
*/
public class Singletance1 {
public static void main(String[] args){
Singletance1 s1=Singletance1.getSingletance();
Singletance1 s2=Singletance1.getSingletance();
System.out.println(s1.hashCode()+" "+s2.hashCode());
}
private Singletance1(){
}
private static Singletance1 instance=null;
public static synchronized Singletance1 getSingletance(){
if(instance==null){
instance=new Singletance1();
}
return instance;
}
public static Singletance1 getSingletance1(){
synchronized (Singletance1.class){
if(null==instance){
instance=new Singletance1();
}
return instance;
}
}
public static Singletance1 getSingletance2(){
if(null==instance){
synchronized (Singletance1.class){
if(null==instance)
instance=new Singletance1();
}
}
return instance;
}
}
饿汉式:
/*
*创建并初始化一个静态私有对象,构造函数私有化
*,提供一个对外的静态共有方法用于获取该实例对象
*/
public class Singletance2 {
private static Singletance2 instance=new Singletance2();
private Singletance2(){
}
public static Singletance2 getInstance(){
return instance;
}
}
生产者消费者模式:使用管程或信号灯来解决死锁问题
package TestThread;
/**
* Created by lenovo on 2016/1/22.
*/
public class Movie {
/**
* flag=true 生产者生产,消费者等待,生产完成后通知消费者消费
* flag=false 消费者消费,生产者等待,消费消费后通知生产者生产
*/
private String pic;
private boolean flag=true;
、
public synchronized void play(String pic){
if(!flag){
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("播放了"+pic);
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
this.pic=pic;
this.notify();
this.flag=false;
}
public synchronized void watch(){
if(flag){
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("观看了"+this.pic);
this.notifyAll();
this.flag=true;
}
}
package TestThread;
/**
* Created by lenovo on 2016/1/22.
*/
public class Player implements Runnable{
private Movie movie;
public Player(Movie movie) {
this.movie = movie;
}
@Override
public void run() {
for(int i=0;i<20;i++){
if(i%2==0){
movie.play("十面埋伏");
}else{
movie.play("霸王别姬");
}
}
}
}
package TestThread;
/**
* Created by lenovo on 2016/1/22.
*/
public class Watcher implements Runnable{
private Movie movie;
public Watcher(Movie movie) {
this.movie = movie;
}
@Override
public void run() {
for(int i=0;i<20;i++){
movie.watch();
}
}
}
package TestThread;
/**
* Created by lenovo on 2016/1/22.
*/
public class Cinema {
public static void main(String[] args){
Movie movie=new Movie();
Player player=new Player(movie);
Watcher watcher=new Watcher(movie);
new Thread(player).start();
new Thread(watcher).start();
}
}
Timer定时器类
方法:schedule(TimerTask task,Date time)
schedule(TimerTask task,Date startTime,long period)
TimerTask任务类
该类是一个线程类,需要重写run方法,
public class TestTimer {
public static void main(String[] args){
Timer timer=new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
System.out.println("print once at 10s");
}
},new Date(System.currentTimeMillis()+10000),1000L);
}
}