多线程-同步代码块

//copyright?liupengcheng
//http://www.cnblogs.com/liupengcheng

/**
* Created by Administrator on 2014/10/24.
* synchronized 锁旗标,改变标志位,已达到同一时刻只有一个线程在执行有效操作。
*/

//copyright?liupengcheng
//http://www.cnblogs.com/liupengcheng

public class TheadDemo5 {
    public static void main(String[] args)
    {
        ThreadTest t = new ThreadTest();
        Thread t1 = new Thread(t);t1.start();
        Thread t2 = new Thread(t);t2.start();
        Thread t3 = new Thread(t);t3.start();
    }
}

//copyright?liupengcheng
//http://www.cnblogs.com/liupengcheng

class ThreadTest implements Runnable {
    private int tickets = 100;
    String str = new String("");

    public void run() {
        while (true) {
            synchronized (str) {
                if (tickets > 0) {
                    try {
                        Thread.sleep(10);
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                    }
                    System.out.println(Thread.currentThread().getName() + "is saling ticket" + tickets--);
                }
            }
        }
    }
}

//copyright?liupengcheng
//http://www.cnblogs.com/liupengcheng

/**
* 结果为
* Thread-0is saling ticket100
Thread-0is saling ticket99
Thread-0is saling ticket98
Thread-0is saling ticket97
Thread-0is saling ticket96
Thread-0is saling ticket95
Thread-2is saling ticket94
Thread-2is saling ticket93
Thread-2is saling ticket92
Thread-2is saling ticket91
Thread-2is saling ticket90
Thread-2is saling ticket89
Thread-2is saling ticket88
Thread-2is saling ticket87
Thread-2is saling ticket86
Thread-2is saling ticket85
Thread-2is saling ticket84
Thread-2is saling ticket83
Thread-2is saling ticket82
Thread-2is saling ticket81
Thread-2is saling ticket80
Thread-2is saling ticket79
Thread-2is saling ticket78
Thread-2is saling ticket77
Thread-2is saling ticket76
Thread-2is saling ticket75
Thread-2is saling ticket74
Thread-2is saling ticket73
Thread-2is saling ticket72
Thread-2is saling ticket71
Thread-2is saling ticket70
Thread-2is saling ticket69
Thread-2is saling ticket68
Thread-2is saling ticket67
Thread-2is saling ticket66
Thread-2is saling ticket65
Thread-2is saling ticket64
Thread-2is saling ticket63
Thread-2is saling ticket62
Thread-2is saling ticket61
Thread-2is saling ticket60
Thread-2is saling ticket59
Thread-2is saling ticket58
Thread-2is saling ticket57
Thread-2is saling ticket56
Thread-2is saling ticket55
Thread-2is saling ticket54
Thread-2is saling ticket53
Thread-2is saling ticket52
Thread-2is saling ticket51
Thread-2is saling ticket50
Thread-2is saling ticket49
Thread-2is saling ticket48
Thread-2is saling ticket47
Thread-2is saling ticket46
Thread-2is saling ticket45
Thread-2is saling ticket44
Thread-2is saling ticket43
Thread-2is saling ticket42
Thread-2is saling ticket41
Thread-2is saling ticket40
Thread-2is saling ticket39
Thread-2is saling ticket38
Thread-2is saling ticket37
Thread-2is saling ticket36
Thread-2is saling ticket35
Thread-2is saling ticket34
Thread-2is saling ticket33
Thread-2is saling ticket32
Thread-2is saling ticket31
Thread-2is saling ticket30
Thread-2is saling ticket29
Thread-2is saling ticket28
Thread-2is saling ticket27
Thread-2is saling ticket26
Thread-2is saling ticket25
Thread-2is saling ticket24
Thread-2is saling ticket23
Thread-2is saling ticket22
Thread-2is saling ticket21
Thread-2is saling ticket20
Thread-2is saling ticket19
Thread-2is saling ticket18
Thread-2is saling ticket17
Thread-2is saling ticket16
Thread-2is saling ticket15
Thread-2is saling ticket14
Thread-2is saling ticket13
Thread-2is saling ticket12
Thread-2is saling ticket11
Thread-2is saling ticket10
Thread-2is saling ticket9
Thread-2is saling ticket8
Thread-2is saling ticket7
Thread-2is saling ticket6
Thread-2is saling ticket5
Thread-2is saling ticket4
Thread-2is saling ticket3
Thread-2is saling ticket2
Thread-2is saling ticket1

*/

//copyright?liupengcheng
//http://www.cnblogs.com/liupengcheng

时间: 2024-10-14 17:20:38

多线程-同步代码块的相关文章

JAVA之旅(十三)——线程的安全性,synchronized关键字,多线程同步代码块,同步函数,同步函数的锁是this

JAVA之旅(十三)--线程的安全性,synchronized关键字,多线程同步代码块,同步函数,同步函数的锁是this 我们继续上个篇幅接着讲线程的知识点 一.线程的安全性 当我们开启四个窗口(线程)把票陆陆续续的卖完了之后,我们要反思一下,这里面有没有安全隐患呢?在实际情况中,这种事情我们是必须要去考虑安全问题的,那我们模拟一下错误 package com.lgl.hellojava; import javax.security.auth.callback.TextInputCallback

多线程(同步代码块和同步函数)

线程安全问题 当多条语句在操作同一个线程共享数据时,一个线程对多条语句只执行了一部分,还没有执行完,    另一个线程参与进来执行.导致共享数据的错误. 解决办法:    对多条操作共享数据的语句,只能让一个线程都执行完.在执行过程中,其他线程不可以参与执行. Java对于多线程的安全问题提供了专业的解决方式.就是同步代码块. synchronized(对象){    需要被同步的代码}对象如同锁.持有锁的线程可以在同步中执行.没有持有锁的线程即使获取cpu的执行权,也进不去,因为没有获取锁.

多线程之同步代码块与同步函数

/*需求:买票分析:多个窗口卖票 是并发的,是多线程利用Runnable接口,传入一个对象,开启多线程 */class Ticket implements Runnable{ private int num=100; Object obj=new Object(); public void run(){ while(true){ synchronized(obj) { if(num>0) { System.out.println(num--+Thread.currentThread().getN

java中多线程模拟(多生产,多消费,Lock实现同步锁,替代synchronized同步代码块)

import java.util.concurrent.locks.*; class DuckMsg{ int size;//烤鸭的大小 String id;//烤鸭的厂家和标号 DuckMsg(){ } DuckMsg(int size, String id){ this.size=size; this.id=id; } public String toString(){ return id + " 大小为:" + size; } } class Duck{ private int

java 多线程安全问题-同步代码块

/* 多线程的安全问题: while(true) { if(tick>0) { //线程0,1,2,3在余票为1时,都停滞在这里,之后分别获得CPU执行权,打印出0,-1,-2等错票 System.out.println(Thread.currentThread().getName()+"....sale : "+ tick--); } } 问题的原因: 当多条语句在操作同一个线程共享数据时,一个线程对多条语句只执行一部分,还没有执行完,停滞 另一个线程参与进来执行,导致共享数据

Java---13---多线程:解决多线程的安全问题---synchronized 同步代码块

还是之前卖票的例子: class Test implements Runnable { private int num = 50; Object obj = new Object(); public void run () { while (true) { if (num >= 0) { try { Thread.sleep(20); } catch (Exception e) { // TODO: handle exception System.out.println(e.toString()

从头认识多线程-2.8 缓解同步方法的隐患-同步代码块

这一章节我们来讨论一下缓解同步方法的隐患-同步代码块. 1.思路:把同步方法,降低同步的粒度,同步到代码块 2.根据上一章节的例子,我们把代码修改一下 (1)第一种方法,把同步标记移到更新的那一栏里面去,一般来说大部分都是更新的时候需要同步数据 package com.ray.deepintothread.ch02.topic_9; /** * 从头认识多线程-2.8 缓解同步方法的隐患-同步代码块<br> * * @author RayLee * */ public class Relief

[java多线程] - 锁机制&amp;同步代码块&amp;信号量

在美眉图片下载demo中,我们可以看到多个线程在公用一些变量,这个时候难免会发生冲突.冲突并不可怕,可怕的是当多线程的情况下,你没法控制冲突.按照我的理解在java中实现同步的方式分为三种,分别是:同步代码块机制,锁机制,信号量机制. 一.同步代码块 在java的多线程并发开发过程中,我们最常用的方式就是使用同步代码关键字(synchronized).这种方式的使用不是特别复杂,需要注意的只是你需要明确到底同步的是那个对象,只有当同步的对象一致的情况下,才能够控制互斥的操作.一般情况下,我们会同

多线程之同步代码块

class MyThreadimplements Runnable{ private int ticket = 6; @Override public void run(){ for(int i = 0;i < 10;i++){ synchronized(this){  //同步代码块 if(this.ticket>0){ try{ Thread.sleep(1000);  //线程休眠 }catch (InterruptedException e) { e.printStackTrace()