黑马程序员——多线程
来源:互联网 发布:股票选股器软件下载 编辑:程序博客网 时间:2024/06/04 20:07
------- <a href="http://www.itheima.com" target="blank">android培训</a>、<a href="http://www.itheima.com" target="blank">java培训</a>、期待与您交流! ----------
多线程:
进程;进程就是应用程序运行的时候,占有的存储空间。
线程;线程是进程中的一个子程序,对于CPU独立的执行路径。
第一种继承方式,继承Therad,重写run方法,建立子类对象,调用start();方法
代码:
class Join implements Runnable{
public void run(){
for(int x = 0 ; x < 50 ; x++)
System.out.println(Thread.currentThread().getName()+"run..."+x);
}
}
public class ThreadJoinDemo {
public static void main(String[] args)throws Exception {
Join j = new Join();
Thread t0 = new Thread(j);
Thread t1 = new Thread(j);
t0.start();
t0.join();
t1.start();
for(int x = 0 ; x < 50 ; x++)
System.out.println(Thread.currentThread().getName()+"main..."+x);
}
}
第二种实现方法:
实现Runnable接口,重写run方法,建立Thread对象传递Runnable接口的实例对象。
Thread对象调用start方法。
设置和获取线程名字:
Thread类的gerName(),
Thread类的静态方法currentTherad(),getName()非Thread子类中使用。
Thread类的setName()方法。
Thread类的构造方法
代码举例:
//定义资源对象,成员变量,私有!
class Resource{
private String name;
private String sex;
private boolean b = false;
//对变量赋值
public synchronized void set(String name,String sex){
if(b)
try{this.wait();}catch(Exception e){}
this.name = name;
this.sex = sex;
b = true;
this.notify();
}
//直接打印变量值
public synchronized void get(){
if(!b)
try{this.wait();}catch(Exception e){}
System.out.println(name+".."+sex);
b = false;
this.notify();
}
}
//定义输入线程,
class Input implements Runnable{
private Resource r;
Input(Resource r){this.r = r;}
public void run(){
int x = 0 ;
while(true){
if(x % 2 == 0){
r.set("张三", "男");
}else{
r.set("lisi", "nv");
}
x++;
}
}
}
//定义输出线程,打印
class Output implements Runnable{
private Resource r;
Output(Resource r){this.r = r;}
public void run(){
while(true)
r.get();
}
}
public class ThreadDemo {
public static void main(String[] args) {
Resource r = new Resource();
Input in = new Input(r);
Output out = new Output(r);
Thread tin = new Thread(in);
Thread tout = new Thread(out);
tin.start();
tout.start();
}
}
线程的安全隐患:
多线程操作共享数据,存在安全隐患;
使用同步代码块,同步的方法解决存在的安全隐患问题;
同步的原理,锁的原理,持有锁的对象可以在同步中运行,没有锁的对象则挡在同步以外;
保证是多线程操作共享数据,锁是只有一个;
静态方法的锁是类名.class,非静态方法的锁是this;
代码解决安全隐患问题;
class Ticket implements Runnable {
private int ticket = 100;
private Object obj = new Object();
public void run() {
while (true) {
synchronized (obj) {
if (ticket > 0) {
try {
Thread.sleep(10);
} catch (Exception e) {}
System.out.println(Thread.currentThread().getName()+ " 出售第。。" + ticket--);
}
}
}
}
}
public class ThreadDemo2 {
public static void main(String[] args) {
// 创建Thread对象,传递Runnable接口的实现类对象
Ticket t = new Ticket();
Thread t1 = new Thread(t);
Thread t2 = new Thread(t);
Thread t3 = new Thread(t);
Thread t4 = new Thread(t);
t1.start();
t2.start();
t3.start();
t4.start();
}
}
线程的通信:
多线程操作共享数据的方向不同;
线程的等待与唤醒机制,实现间隔输出;
wait notify 必须有同步,必须是锁调用;
等待唤醒写在Objec类中;
线程的停止方式:
利用变量,结束run方法;
利用异常结束run方法,Thread类的interrupt方法;
代码举例:
class Stop implements Runnable{
private boolean flag = true;
public void run(){
while(flag){
synchronized(this){
try{
this.wait();
}catch(Exception e){
System.out.println("线程被打了");
flag = false;
}
System.out.println("run...");
}
}
}
public void setFlag(boolean flag){
this.flag = flag;
}
}
public class ThreadStopDemo {
public static void main(String[] args) {
Stop s = new Stop();
Thread t = new Thread(s);
t.start();
for(int x = 0 ; x < 100 ; x++){
if(x == 99)
//s.setFlag(false);
t.interrupt();
System.out.println("main..."+x);
}
}
}
线程状态图:
还是有很多不懂,只能写到这里了!
没有学不好,只有不认真~~——至自己!!
0 0
- 黑马程序员—多线程
- 黑马程序员—多线程
- 黑马程序员—多线程
- 黑马程序员—多线程
- 黑马程序员—多线程
- 黑马程序员—多线程
- 黑马程序员—多线程
- 黑马程序员—多线程
- 黑马程序员—多线程
- 黑马程序员—多线程
- 黑马程序员—多线程
- 黑马程序员—多线程
- 黑马程序员—多线程
- 黑马程序员—多线程
- 黑马程序员—多线程
- 黑马程序员—多线程
- 黑马程序员—多线程
- 黑马程序员—多线程
- 全局变量和局部变量是否可以重名(笔试题)
- 机房收费系统总结
- AIX patch 的升级和回退
- onerror
- 利用Quartz 2D实现图片的旋转、缩放、裁剪
- 黑马程序员——多线程
- Android Fragment 总结
- java读取excel数据,导致内存泄露的问题
- 工程师的生活
- 树莓派开发系列教程4——树莓派网络与更新配置(有线、无线)
- 收藏的Android非常好用的组件或者框架(转)
- PROFILE_IDC LEVEL_IDC
- strlen
- vb 中recordset提示对象关闭时不允许操作