黑马程序员——java中的线程

来源:互联网 发布:行知职高有什么专业 编辑:程序博客网 时间:2024/05/29 05:17

----------- android培训、java培训、java学习型技术博客、期待与您交流! ------------

线程:
1.线程概念

线程:一个程序里面不同的执行路径;
2.创建和启动线程的的方式
每个线程通过创建一个Thread的实例来创建线程和调用thread对象中的run方法来完成操作,并通过调用star()方法来启动;
3.sleep
public class Number1 {
    
public static void main(String[] args)  {
mythread m=new mythread();//实例化一个继承线程的类
m.start();//开始进程
try{
Thread.sleep(10000);//thread实现的是主线程
}
catch(InterruptedException e){

}
m.ting();//停止线程
}

}
class mythread extends Thread {//继承线程类 
private boolean fale=true;
public void ting(){//停止线程方法ting
fale=false;
}
public void run(){//线程运行
while(fale==true){
System.out.println("NUMBER: "+"---"+" "+new Date());
try{
Thread.sleep(1000);
}
catch(InterruptedException e){
return;
}

  }
}
}
   
4.join(主线程合并)
public class TestJoin {
  public static void main(String[] args) {
    MyThread2 t1 = new MyThread2("abcde");
    t1.start();
    try {
    t1.join();
    } catch (InterruptedException e) {}
   
    for(int i=1;i<=10;i++){
      System.out.println("i am main thread");
    }
  }
}
class MyThread2 extends Thread {
  MyThread2(String s){
  super(s);
  }
  
  public void run(){
    for(int i =1;i<=10;i++){
      System.out.println("i am "+getName());
      try {
      sleep(1000);
      } catch (InterruptedException e) {
      return;
      }
    }
  }
}




5.yiedl
public class TestYield {
  public static void main(String[] args) {
    MyThread3 t1 = new MyThread3("t1");
    MyThread3 t2 = new MyThread3("t2");
    t1.start(); t2.start();
  }
}
class MyThread3 extends Thread {
  MyThread3(String s){super(s);}
  public void run(){
    for(int i =1;i<=100;i++){
      System.out.println(getName()+": "+i);
      if(i%10==0){
        yield();
      }
    }
  }
}








线程同步:
6.synchronized(锁)

锁:关键字synchronized
锁是在原有进程结束之后,才能进行其他进程
public  class Text implements Runnable{
Times time=new Times();
public static void main(String[] args) {
        Text t=new Text();
Thread t1=new Thread(t);
Thread t2=new Thread(t);
t1.setName("t1");
t2.setName("t2");
t1.start();
t2.start();
}
@Override
public void run() {
// TODO Auto-generated method stub
time.add(Thread.currentThread().getName());
 }
}


class Times {
public static int num=0;
public synchronized void add(String name){
num++;
try{
Thread.sleep(1);
}
catch(InterruptedException e){

}
System.out.println(name+"这是第 "+num);
}

}
死锁:
public class Number1 implements Runnable{
    public int fale;
    public Object o1=new Object();
    public Object o2=new Object();
    
public static void main(String[] args)  {
        Number1 num1=new Number1();
Number1 num2=new Number1();
Thread t1=new Thread(num1);
Thread t2=new Thread(num2);
        num1.fale=1;
        num2.fale=2;
        t1.start();
        t2.start();
        


}


@Override
public void run() {
if(fale==1){
synchronized (o1) {
try{
Thread.sleep(500);
}
catch(Exception e){
e.printStackTrace();
}
}
synchronized(o2){
try{
Thread.sleep(1000);
}
catch(Exception e){
e.printStackTrace();
}
}
System.out.println("1");
}
if(fale==2){
synchronized(o1){
try{
Thread.sleep(500);
}
catch(Exception e){
e.printStackTrace();
}
}
synchronized(o2){
try{
Thread.sleep(500);
}
catch(Exception e){
e.printStackTrace();
}
}
System.out.println("2");
}
}

}


生产消费线程实例:
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(p).start();
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();
}
}
this.notifyAll();
arrWT[index] = wt;
index ++;
}

public synchronized WoTou pop() {
while(index == 0) {
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
this.notifyAll();
index--;
return arrWT[index];
}
}


class Producer implements Runnable {
SyncStack ss = null;
Producer(SyncStack ss) {
this.ss = ss;
}

public void run() {
for(int i=0; i<20; i++) {
WoTou wt = new WoTou(i);
ss.push(wt);
System.out.println("生产了:" + wt);
try {
Thread.sleep((int)(Math.random() * 200));
} 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++) {
WoTou wt = ss.pop();
System.out.println("消费了: " + wt);
try {
Thread.sleep((int)(Math.random() * 1000));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}



----------- android培训、java培训、java学习型技术博客、期待与您交流! ------------