---------------------- <a href="http://edu.csdn.net/heima" target="blank">android培训</a>、<a href="http://edu.csdn.net/heima" target="blank">java培训</a>、期待与您交流! ----------------------
线程状态生命周期:
现场状态生命周期分为五个阶段,
1 新建 用Thread类或子类创建
2 就绪 调用start(),运行条件具备,获得CPU即可运行
3 运行 获得CPU后,运行Thread子类中的run(),用子类重写覆盖父类。
4 中断 有四种情况
a JVM将CPU资源从当前线程切换到其他线程
b 执行sleep(int millssecond),经过MS后重新获得使用权
c 执行wait()直到notify()重新排队等待CPU资源
d 使用CPU间,执行某操作进入阻塞
5 死亡 线程释放内从单元 正常结束及非正常结束
两种创建线程的方法
a 继承Thread类创建
public class ThreadDemo extends Thread(){
public void run(){};
public static void main(String []args){
new Thread().start();
}
}
public static void main (String[] args) throws Exception{
TestThread tt = new TestThread();
tt.start();
tt.start();
tt.start();
tt.start();
}
public class TestThread extends Thread{
int tickets = 100;
public void run(){
while(true){
if(tickets > 0)
System.out.println("run(): "+ Thread.currentThread().getName() + "is sailing :" + tickets--);
}
}
}
b 实现Runnable创建 支持多重继承
public class RunnableDemo implements Runnable{
public void run(){}
public static void main(){
new Thread(new RunnableDemo()).start();
}
}
public static void main (String[] args) throws Exception{
TestThread tt = new TestThread();
new Thread(tt).start();
new Thread(tt).start();
new Thread(tt).start();
new Thread(tt).start();
}
public class TestThread implements Runnable{// extends Thread{
int tickets = 100;
public void run(){
while(true){
if(tickets > 0)
System.out.println("run(): "+ Thread.currentThread().getName() + "is sailing :" + tickets--);
}
}
}
注意:具有相同目标的线程,每个线程都有自己的内从单元,局部变量值不相互影响。
线程常用方法:
setName()
getName()
currentThread() 获取当前正在执行的线程
以下两条语句效果相同
Thread t = Thread.currentThread(); t.getNmae;
String str = Thread.currentThread.getNmae();
interrupt() 用来中断正在休眠的线程,不能中断正在执行的线程
进行终止:在run(){}内使用return
用stop()强制中断,此操作不安全
线程联合:一个线程等到另一个线程执行完毕才可运行,可使用join()。
例如:A是当前正在运行的线程,它联合线程B(即A运行间,执行B.join())则线程A中断,直到B执行完毕。若B已经结束再执行B.join()没用
守护线程:称用户线程,主要是为其他线程提供支持。若非守护线程执行完毕,只有守护线程,则退出程序。
线程调用setDaemon(true)可将本身设置为守护线程,必须在运行前设置,默认非守护
public static void main(String[] args) {
//new Thread().start();
//new TestThread().run();
//new TestThread().start();
Thread tt = new TestThread();
tt.setDaemon(true);
tt.start();
int index = 0;
/*while(true){
if(index++ == 100)
tt.join(1000);
System.out.println("main(): "+ Thread.currentThread().getName());
}*/
}
多线程的调用
package com.edu.swu;
public class ThreadDwmol1 {
/**
* @param args
*/
public static void main(String[] args) {
//new Thread().start();
//new TestThread().run();
new TestThread().start();
while(true){
System.out.println("main(): "+ Thread.currentThread().getName());
}
}
}
package com.edu.swu;
public class TestThread extends Thread{
public void run(){
while(true){
System.out.println("run(): "+ Thread.currentThread().getName());
}
}
}
多窗口售票系统
只能创建一个资源对象,线程共同卖N张票
TestThread tt = new TestThread();
tt.start();
tt.start();
Runnble 用多线程处理同一资源
TestThread tt = new TestThread();
new Thread(tt).start();
new Thread(tt).start();
使用多线程具有交互作用,如拷贝文件过程中的取消。
线程间同步使用synchronized 关键字
String str = new String("Holle");
synchronized(str){} 任意类型的对象皆可,同步语句。
synchronized关键字可用于函数方法,代码块,类的同步,是要使用同一同步对象就可实现不同方法,代码块之间的同步。
使用synchronized会增大开销,速度变慢,原因是不断检查锁旗标。
public class TestThread implements Runnable{// extends Thread{
int tickets = 100;
String str = new String("");
public void run(){
while(true){
synchronized(str){
if(tickets > 0)
/*try{
Thread.sleep(2000);
}
catch(Exception e){}*/
System.out.println("run(): "+ Thread.currentThread().getName() + "is sailing :" + tickets--);
}
}
}
}
也可让同步方法与同步语句块间同步,代码中检查同步对象用this,synchronized(this);
同步监视器死锁:
synchronized(str){语句块}
synchronized(this){}
synchronized void sale(){
synchronized(str){}
}
线程间通信
wait() 告诉当前线程放弃监视器直到notify为止
notify() 唤醒同一监视器对象中的第一个wait()对象
notifyAll() 唤醒同一监视器中的所有wait(),高优先级先被唤醒执行
synchronized(o) 线程t得到对象o的锁旗标
o.wait() o.notify() 用对象O调用
package com.edu.swu;
public class Producer implements Runnable
{
Q q;
public Producer(Q q) {
super();
this.q = q;
}
public void run()
{
int i = 0;
while(true)
{
synchronized (q)
{
if(q.bFull)
try {
q.wait();
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
if(i == 0)
{
q.name = "zhangsan";
try
{
Thread.sleep(1);
}
catch(Exception e)
{
}
q.sex = "male";
}
else
{
q.name = "lisi";
q.sex = "female";
}
Boolean bFull = true;
q.notify();
}
i = (i+1)%2;
}
}
}
package com.edu.swu;
public class Consumer implements Runnable
{
Q q;
public Consumer(Q q) {
super();
this.q = q;
}
public void run()
{
while(true)
{
synchronized (q)
{
if(!q.bFull)
try {
q.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.print(q.name);
System.out.println(":" + q.sex);
Boolean bFull = false;
q.notify();
}
}
}
}
package com.edu.swu;
public class Q {
String name = "unknown";
String sex = "unknown";
boolean bFull = false;
}
package com.edu.swu;
public class ThreadConmunication
{
/**
* @param args
*/
public static void main(String[] args)
{
Q q = new Q();
new Thread(new Producer(q)).start();
new Thread(new Consumer(q)).start();
}
}
---------------------- <a href="http://edu.csdn.net/heima" target="blank">android培训</a>、<a href="http://edu.csdn.net/heima" target="blank">java培训</a>、期待与您交流! ----------------------
详细请查看:<a href="http://edu.csdn.net/heima" target="blank">http://edu.csdn.net/heima</a>