java应用技术 1(1)

java应用技术
1(1)
一、介绍MyEclipse
1.快捷方式:
整理格式(ctrl+shift+F)
引入包(ctrl+shift+O)
Ctrl+T查看类的结构
代码提示(alt+/)
二、多线程:
1.继承Thread类

子类覆盖父类中的run方法,将线程运行
的代码存放在run中。
建立子类对象的同时线程也被创建。
通过调用start方法开启线程。
案例:
public class ThreadDemo {
public static void main(String[] args) {
Test t1 = new Test("haoren");
t1.start();
Test t2 = new Test("huairen");
t2.start();
}
}
class Test extends Thread{
private String name;
public  Test(String name){
this.name = name;
}
public void run(){
for(int i=0;i<10;i++){
System.out.println(name+":"+i);
}
}
}


2.用Thread的注意事项

必须用start()方法启动;
native 表示要调用操作系统的函数
;
多次调用start()会报异常
;
3.线程的4种状态
1.启用(start);
2.等待(wait);
2.睡眠(sleep);
3.结束(stop);
4.实现Runnable接口
1.子类覆盖接口中的run方法;


2.通过Thread类创建线程,并将实现了Runnable接口的子类对象作为参数传递给Thread类的构造函数。
3.Thread类对象调用start方法开启线程。
5.Thread和Runnable的关系


1.Thread是Runnable 的子类
6.Thread和Runnable 的区别


1.Runnable可以共享数据
7.线程操作的常用方法


1.取得和设置线程名字getName();
2.判断线程是否启动isAlive();


3.isAlive() 判断线程是否处于活动状态;
4.sleep(long millis) 线程休眠


5.join() 强制执行线程


6.interrupt() 中断线程的某种状态


8.同步(synchronized)
1.同步可以解决安全问题的根本原因就在那个对象上。


该对象如同锁的功能。


案例:生产赵日天
package cn.java.java1;


public class PSThreadDemo {
public static void main(String[] args) {
Prodution p = new Prodution();
Producter pr = new Producter(p);
saler s = new saler(p);
Thread th1 = new Thread(pr);
Thread th2 = new Thread(s);
th1.setName("生产窗口:");
th2.setName("销售窗口:");
th1.start();
th2.start();
}
}


class Prodution {
private String name;
private int i ;
private boolean flag = false;
public Prodution() {
}

public Prodution(boolean flag) {
this.flag = flag;
}

public Prodution(String name, int i) {
this.name = name;
this.i = i;
}

public boolean isFlag() {
return flag;
}


public void setFlag(boolean flag) {
this.flag = flag;
}


public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getI() {
return i;
}
public void setI(int i) {
this.i = i;
}
}


class Producter implements Runnable {
private Prodution p = null;
private int i = 1;

public Producter() {
}
public Producter(Prodution p) {
this.p = p;
}

public Prodution getP() {
return p;
}
public void setP(Prodution p) {
this.p = p;
}
@Override
public void run() {
// TODO Auto-generated method stub
for(int b = 0;b<100;b++){
synchronized (p) {
if(p.isFlag()){
try {
p.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
p.setName("赵日天 ");
p.setI(i++);
System.out.println(Thread.currentThread().getName()+"生产:"+p.getName()+p.getI()+"号");
p.notify();
p.setFlag(true);
}

}

}

}


class saler implements Runnable {
private Prodution p = null;
public saler(){}
public saler(Prodution p) {
this.p = p;
}


public Prodution getP() {
return p;
}
public void setP(Prodution p) {
this.p = p;
}
@Override
public void run() {
// TODO Auto-generated method stub
for(int b = 0;b<100;b++){
synchronized (p) {
if(!p.isFlag()){
try {
p.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.println(Thread.currentThread().getName()+"售出:"+p.getName()+p.getI()+"号");
p.notify();
p.setFlag(false);
}

}

}

}
9.同步的特点
1.同步需要两个或者两个以上的线程。
多个线程使用的是同一个锁。
2.未满足这两个条件,不能称其为同步。
3.同步的弊端:当线程相当多时,因为每个线程都会去判断
同步上的锁,这是很耗费资源的
10.同步函数


1.在函数上加上synchronized修饰符即可。
2.同步函数的锁是this,而同步代码块的锁可以是任意对象


案例:买票
package cn.java.java1;


public class BuyTicket {
public static void main(String[] args) {
MyThread m1 = new MyThread();
Thread th1 = new Thread(m1);
Thread th2 = new Thread(m1);
th1.setName("买票窗口1    ");
th2.setName("买票窗口2    ");
th1.start();
th2.start();
System.out.println(System.currentTimeMillis());
}
}


class MyThread implements Runnable {
private int tickets = 10;


@Override
public void run() {
while (true) {
fun();
}
}
public synchronized void fun() {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (tickets > 0) {
System.out.println(Thread.currentThread().getName() + "买了:"
+ tickets-- + "  号票");
}
}
}
11.死锁
1.同步应用过多就有可能会出现死锁,所同步不要使用过分太多。


案例:
package cn.java.java1;


public class Thread3 {
public static void main(String[] args) {
Lock l1 = new Lock(true);
Lock l2 = new Lock(false);
Thread th1 = new Thread(l1);
Thread th2 = new Thread(l2);
th1.start();
th2.start();
}
}


class LockFactory {
public static Object ob1 = new Object();
public static Object ob2 = new Object();
}


class Lock implements Runnable {
private boolean flag = false;


public Lock(boolean flag) {
this.flag = flag;
}


@Override
public void run() {
if (!flag) {
while (true) {
synchronized (LockFactory.ob1) {
System.out
.println(Thread.currentThread().getName() + "ob1");
synchronized (LockFactory.ob2) {
System.out.println(Thread.currentThread().getName()
+ "ob2");
}
}
}
} else {
while (true) {
synchronized (LockFactory.ob2) {
System.out
.println(Thread.currentThread().getName() + "ob2");
synchronized (LockFactory.ob1) {
System.out.println(Thread.currentThread().getName()
+ "ob1");
}
}
}
}
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值