java38java中的线程(三)
来源:互联网 发布:淘宝 排除关键字 编辑:程序博客网 时间:2024/06/05 15:32
- 多线程的数据安全
- 同步线程的方法
- 消费者生产者方法
多线程的数据安全
线程体:线程运行时要实现的真正功能的代码。
//实现runnable接口class MyThread implements Runnable{ int i = 100; public void run(){ while (true){ synchronized (this){ //Thread.currentThread()获取当前代码在哪一个线程中运行 System.out.println(Thread.currentThread().getName() + i); i--; Thread.yield(); if(i < 0){ break; } } } }}class Test1{ public static void main(String[] args){ MyThread myThread = new MyThread(); //生成两个Thread对象,但是这两个Thread对象公用同一个线程体 Thread t1 = new Thread(myThread); Thread t2 = new Thread(myThread); //每一个线程都有自己的名字,可以通过setName设置线程名字也可以使用getName 获取名字。 t1.setName("线程a"); t2.setName("线程b"); t1.start(); t2.start(); }}
线程的参数必须为对象,Runnable为线程体接口
同步线程的方法
使用synchronized(this){}来为线程加上同步锁:
作用是线程一执行时获得了线程锁的使用权,其他线程在运行时遇到线程锁将等待至锁内的代码块结束后才能执行并获得锁的执行权。
但是当线程一执行到循环结束到break阶段时候,线程二执行时i的值为-1执行完打印语句之后才开始跳出,出现错误。
消费者生产者方法
//生产者消费者问题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 ManTou { int id; ManTou(int id) { this.id = id; } public String toString() { return "ManTou" + id; }}//堆栈型篮子class SyncStack { int index = 0; ManTou[] arr = new ManTou[6]; //锁住该方法,使修改的对象唯一 public synchronized void push(ManTou mt) { while(index == arr.length) { try{ //Object类中的wait方法,使当前线程等待并放开同步锁。 this.wait(); } catch(InterruptedException e) { e.printStackTrace(); } } //叫醒正在wait的其他线程。 this.notify(); arr[index] = mt; index++; } //拿出馒头 public synchronized ManTou pop(){ while(index == 0) { try{ this.wait(); } catch(InterruptedException e) { e.printStackTrace(); } } //叫醒其他正在wait的线程 this.notify(); index--; return arr[index]; }} //生产者class Producer implements Runnable { SyncStack ss = null; //确定往哪个筐子里边放馒头 Producer(SyncStack ss) { this.ss = ss; } public void run() { for(int i = 0;i < 20;i++) { ManTou mt = new ManTou(i); ss.push(mt); System.out.println("生产者 : " + mt); try{ Thread.sleep(10); } catch(InterruptedException e) { e.printStackTrace(); } } }}class Consumer implements Runnable { SyncStack ss = null; //确定往哪个筐子里边放馒头 Consumer(SyncStack ss) { this.ss = ss; } public void run() { for(int i = 0;i < 20;i++) { ManTou mt = ss.pop(); System.out.println("消费者 :" + mt); try{ Thread.sleep(1000); }catch(InterruptedException e) { e.printStackTrace(); } } }}
0 0
- java38java中的线程(三)
- C#中的线程(三)多线程
- C#中的线程(三)多线程
- android中的线程(三上)
- C#中的线程(三)使用多线程
- Android中的线程间通信(三)
- Delphi中的线程类三
- Java中的线程(三)-Java的锁机制
- Java 中的线程(三)-- MarsChen Java4Android 教程学习笔记
- java中的线程(三)——控制线程的几种简单的方法和线程同步
- C#中的线程(三) 使用多线程
- C#中的线程(三) 使用多线程
- C#中的线程(三) 使用多线程
- C#中的线程(三) 使用多线程
- C#中的线程(三) 使用多线程
- C#中的线程(三) 使用多线程
- C#中的线程(三) 使用多线程
- 线程基础(三)
- ffmpeg 和 SDL的教程 tutorial3学习--播放声音
- fastjson把对象转化成json避免$ref
- 浅谈java异常[Exception]
- ice入门
- Python标准库之sqlite3使用实例
- java38java中的线程(三)
- 通过反射机制获得某个类中的属性,构造方法,方法
- Velt-0.2.6对Python远程调试的支持
- ICE简单介绍及使用示例
- C++实验6作业
- Android打造属于自己的数据库操作类。
- 《图解HTTP》学习笔记(六)-HTTP首部
- Cocos2d-3.x Action介绍
- 新EventHandler