`
javacto
  • 浏览: 84057 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Thread(下) 【016】

阅读更多
-------TestDeadLock------------------------------------------------------------------------
package com.testthread2;

public class TestDeadLock implements Runnable {
	public int flag  ;
	static Object o1 = new Object() ;
	static Object o2 = new Object() ;
	public void run() {
		System.out.println("flag" + flag) ;
		if(flag == 1) {
			synchronized(o1) {
				try {
					Thread.sleep(500) ;
				} catch (Exception e) {
					e.printStackTrace() ;
				}
				synchronized(o2) {
					System.out.println("1") ;
				}
			}
		}
		if(flag == 0) {
			synchronized(o2) {
				try {
					Thread.sleep(500) ;
				} catch(Exception e) {
					e.printStackTrace();
				}
				synchronized(o1) {
					System.out.println("0") ;
				}
			}			
		}
	}
	
	public static void main(String args[]) {
		TestDeadLock td1 = new TestDeadLock() ;
		TestDeadLock td2 = new TestDeadLock() ;
		td1.flag = 1 ;
		td2.flag = 0 ;
		Thread t1 = new Thread(td1) ;
		Thread t2 = new Thread(td2) ;
		t1.start();
		t2.start();
	}

}



-------TestSynchronized------------------------------------------------------------------------
package com.testthread2;

// 两个进程同时调用同一个方法
public class TestSync implements Runnable{
	Timer timer = new Timer() ;
	public static void main(String args[]) {
		TestSync test = new TestSync() ;
		Thread t1 = new Thread(test) ;
		Thread t2 = new Thread(test) ;
		t1.setName("t1") ;
		t2.setName("t2") ;
		t1.start();
		t2.start();
	}
	public void run() {
		timer.add(Thread.currentThread().getName()) ;
	}
}

class Timer {
	private static int num = 0;

	public synchronized void add(String name) { //用synchronized锁定当前对象
		num++;
		try {
			Thread.sleep(1);
		} catch (InterruptedException e) {

		}
		System.out.println(name + ",,你是第" + num + "个使用timer的线程");

	}
}



-------Synchronized or NoSynchronized------------------------------------------------------------------------

package com.testthread2;

public class TT  implements Runnable {
	int b= 100 ;
	
	public synchronized void m1() throws Exception {
		b = 1000 ;
		Thread.sleep(5000) ;
		System.out.println("b="+b) ;
	}
	
	public synchronized void m2() throws Exception {
//	public void m2() throws Exception {
		Thread.sleep(2500) ;
		b = 2000 ;
	}
	
	public void run() {
		try {
			m1() ;
		} catch (Exception e) {
			e.printStackTrace() ;
		}
	}
	
	public static void main(String args[]) throws Exception {
		TT tt = new TT() ;
		Thread t = new Thread(tt) ;
		t.start();
		
		tt.m2();
		System.out.println(tt.b) ;
	}

}


-------Producer & Consumer ------------------------------------------------------------------------
package com.testthread2;

public class ProducerConsumer {
	public static void main(String args[]) {
		SyncStack ss = new SyncStack() ;
		Producer p = new Producer(ss) ;
		Consumer c = new Consumer(ss) ;
		new Thread(p).start() ;
		new Thread(c).start() ;		
	}

}

class WoTou {
	int id ;
	WoTou(int id) {
		this.id = id ;
	}
	public String toString() {
		return "WoTou :" + id ;
	}
}

class SyncStack {
	int index = 0 ; //装到第几个了
	WoTou[] arrWT = new WoTou[6] ;
	
	public synchronized void push(WoTou wt) { //往筐里放馒头
		while(index == arrWT.length) {
			try {
				this.wait();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}  //当前对象 wait:当前的正在访问该对象的线程wait
		}
		this.notify();
		arrWT[index] = wt ;
		index ++ ;
	}
	
	public synchronized WoTou pop() { //从筐里取出馒头
		while(index == 0) {
			try {
				this.wait();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		this.notify() ;
		index -- ;
		return arrWT[index] ;
	}
}

class Producer implements Runnable {
	SyncStack ss = null ;
	public Producer(SyncStack ss) {
		this.ss =ss ;
	}
	
	public void run() {
		for(int i=0; i<25; i++) { //生产的馒头
			WoTou wt = new WoTou(i) ;
			ss.push(wt) ;
			System.out.println("生产了:" + wt) ;
//			try {
//				Thread.sleep(1000) ;
//			} catch (InterruptedException e) {
//				e.printStackTrace() ;
//			}
		}
	}
}

class Consumer implements Runnable {
	SyncStack ss = null ;
	public Consumer(SyncStack ss) {
		this.ss =ss ;
	}
	
	public void run() {
		for(int i=0; i<20; i++) { //消费的馒头
			WoTou wt = ss.pop();
			System.out.println("消费了:" +wt) ;
//			try {
//				Thread.sleep(1000) ;
//			} catch (InterruptedException e) {
//				e.printStackTrace();
//			}
		}
	}
}
分享到:
评论

相关推荐

    sst25vf016 上移植FATFS0.10文件系统

    本文将深入探讨如何在RT_Thread实时操作系统上移植FATFS 0.10文件系统,并利用SST25VF016B闪存芯片作为存储媒介。SST25VF016B是一款常见的SPI接口闪存芯片,容量为1MB,适用于多种嵌入式系统,而FATFS是一个轻量级的...

    华为大版 HG8xxx 系列光猫固件包 R013-R015-R016

    4. **FW_373_HG8xxxV300R016C10SPC139_china_full_all.bin** 和 **FW_373_HG8xxxV300R016C10SPC150_china_all.bin**:这两个文件都是 R016 版本的固件,但有不同的修订号(SPC139 和 SPC150)。这意味着它们可能包含...

    PHP程序设计-3期(KC016) 1.2.2PHP的安装配置拓展知识.doc

    首先,PHP的版本分为线程安全(Thread Safe,TS)和非线程安全(Non-Thread Safe,NTS)两种。线程安全版本主要是为了解决多线程环境下可能出现的并发问题,而非线程安全版本则在没有线程安全检查的情况下提供更高的...

    【RT-Thread作品秀】打饭机器人-电路方案

    开发环境硬件:正点原子探索者STM32F407开发板,普通机械臂,ESP8266,超声波模块US-016,蓝牙模块HC-06,直流电源模块 RT-Thread版本:RT-thread-nano3.1.3 开发工具及版本:KEIL5 RT-Thread使用情况概述使用了RT-...

    实战教程016—C#项目开发实战教程_代码.zip

    10. **多线程与并发**:C#支持多线程编程,通过Thread类或Task Parallel Library(TPL)可以实现并行处理。理解线程同步、锁机制和异步编程(async/await关键字)对于优化性能和编写响应式应用程序非常关键。 11. *...

    PHP程序设计-3期(KC016) 1.2.1Apache的安装配置拓展知识.doc

    相比之下,FastCGI是一种更稳定且高效的处理机制。它以单一线程执行PHP脚本,不需要进行线程安全检查,从而提高了执行效率。因此,如果选择FastCGI作为PHP的执行方式,推荐使用Non Thread Safe(NTS)版本的PHP。 ...

    PHP程序设计-3期(KC016) 拓展知识2-3 全局变量.doc

    注意这里的`#ifdef ZTS`确保这段代码只在开启Zend Thread Safety (ZTS)时执行,因为`sample4_globals_id`仅适用于线程安全环境。 非线程安全版本则使用我们在前面提到的`sample4_globals`结构体来声明和使用全局...

    串行Flash万能驱动库SFUD.zip

    SFUD (Serial Flash Universal Driver) 串行 Flash 万能驱动库 0、SFUD 是什么 SFUD 是一款开源的串行 SPI Flash 通用驱动库。... STM32F2XX RT-Thread 操作系统平台 标签:SFUD

    JAVA绘图设计.rar_Java绘图设计教程_java 绘图_president4ni_screenjzp_绘图

    通过016_JAVA绘图设计这个章节,你将能够学习到如何在Java环境中规划、设计和实现自己的图形界面,从而提升你的编程技能并拓宽应用范围。无论是简单的游戏、数据可视化工具还是复杂的艺术创作,Java绘图设计都是一个...

    asp.net 动态设置窗体的光标

    在ASP.NET开发中,我们有时需要对用户界面进行精细化控制,比如改变网页中鼠标光标的样式,以提供更好的用户体验。...提供的Example016-动态设置窗体的光标示例代码,将帮助你更好地理解和实践这一技巧。

    teadb:在MySQL中使用Maven和Benchmark的演示

    使用MySQL进行Maven部署和基准测试跑//close all IDE first//Java 11 Versionexport JAVA_HOME=/usr/lib/jvm/java-11-openjdkmvn cleanmvn installmvn exec:java结果VM 2核+ 8G threadCount = 100 , 000 batchCount ...

    深入浅出mfc简体中文版

    視窗類別之註冊與視窗之誕生/ 016 訊息迴路/ 018 視窗的生命㆗樞 - 視窗函式/ 019 訊息映射(Message Map)雛形/ 020 對話盒的運作/ 022 模組定義檔(.DEF) / 024 資源描述檔(.RC) / 024 Windows 程式的生與死/ ...

    C#编程实例代码集合

    Example016-动态设置窗体的光标 Example017-自己绘制菜单 Example018-向窗体的系统菜单添加菜单项 namespace Example018_向窗体的系统菜单添加菜单项 { /// /// Form1 的摘要说明。 /// public class Form1 : ...

    JLink_Windows_V648.zip

    DLL: Windows: Renesas RX: When using FINE interface and disabling ongoining debug mode on debug session close, it could happen that a thread was not exited gracefully, causing handle leaks. Fixed. DLL...

    JDK 1.5的泛型實現(Generics in JDK 1.5)

    JDK 1.5的泛型實現(Generics in JDK 1.5) 1 侯捷觀點 JDK 1.5的泛型實現 ...............本文程式源碼可至侯捷網站下載 .....................#016 ... #017 } 圖 9a / JDK1.5的 java.util.ArrayList源碼 ...

Global site tag (gtag.js) - Google Analytics