欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

java必学必会之线程(2)

程序员文章站 2024-03-07 13:04:21
一、线程的优先级别    线程优先级别的使用范例: package cn.galc.test; public class testthread6 {...

一、线程的优先级别

  java必学必会之线程(2)

线程优先级别的使用范例:

package cn.galc.test;

public class testthread6 {
 public static void main(string args[]) {
 mythread4 t4 = new mythread4();
 mythread5 t5 = new mythread5();
 thread t1 = new thread(t4);
 thread t2 = new thread(t5);
 t1.setpriority(thread.norm_priority + 3);// 使用setpriority()方法设置线程的优先级别,这里把t1线程的优先级别进行设置
 /*
  * 把线程t1的优先级(priority)在正常优先级(norm_priority)的基础上再提高3级 
  * 这样t1的执行一次的时间就会比t2的多很多     
  * 默认情况下norm_priority的值为5
  */
 t1.start();
 t2.start();
 system.out.println("t1线程的优先级是:" + t1.getpriority());
 // 使用getpriority()方法取得线程的优先级别,打印出t1的优先级别为8
 }
}

class mythread4 implements runnable {
 public void run() {
 for (int i = 0; i <= 1000; i++) {
  system.out.println("t1:" + i);
 }
 }
}

class mythread5 implements runnable {
 public void run() {
 for (int i = 0; i <= 1000; i++) {
  system.out.println("===============t2:" + i);
 }
 }
}
  run()方法一结束,线程也就结束了。

二、线程同步

 java必学必会之线程(2) 

synchronized关键字的使用范例:

package cn.galc.test;

public class testsync implements runnable {
 timer timer = new timer();

 public static void main(string args[]) {
 testsync test = new testsync();
 thread t1 = new thread(test);
 thread t2 = new thread(test);
 t1.setname("t1");// 设置t1线程的名字
 t2.setname("t2");// 设置t2线程的名字
 t1.start();
 t2.start();
 }

 public void run() {
 timer.add(thread.currentthread().getname());
 }
}

class timer {
 private static int num = 0;

 public/* synchronized */void add(string name) {// 在声明方法时加入synchronized时表示在执行这个方法的过程之中当前对象被锁定
 synchronized (this) {
  /*
  * 使用synchronized(this)来锁定当前对象,这样就不会再出现两个不同的线程同时访问同一个对象资源的问题了 只有当一个线程访问结束后才会轮到下一个线程来访问
  */
  num++;
  try {
  thread.sleep(1);
  } catch (interruptedexception e) {
  e.printstacktrace();
  }
  system.out.println(name + ":你是第" + num + "个使用timer的线程");
 }
 }
}

线程死锁的问题:

package cn.galc.test;

/*这个小程序模拟的是线程死锁的问题*/
public class testdeadlock implements runnable {
 public int flag = 1;
 static object o1 = new object(), o2 = new object();

 public void run() {
 system.out.println(thread.currentthread().getname() + "的flag=" + flag);
 /*
  * 运行程序后发现程序执行到这里打印出flag以后就再也不往下执行后面的if语句了 
  * 程序也就死在了这里,既不往下执行也不退出
  */

 /* 这是flag=1这个线程 */
 if (flag == 1) {
  synchronized (o1) {
  /* 使用synchronized关键字把对象01锁定了 */
  try {
   thread.sleep(500);
  } catch (interruptedexception e) {
   e.printstacktrace();
  }
  synchronized (o2) {
   /*
   * 前面已经锁住了对象o1,只要再能锁住o2,那么就能执行打印出1的操作了 
   * 可是这里无法锁定对象o2,因为在另外一个flag=0这个线程里面已经把对象o1给锁住了 
   * 尽管锁住o2这个对象的线程会每隔500毫秒睡眠一次,可是在睡眠的时候仍然是锁住o2不放的
   */
   system.out.println("1");
  }
  }
 }
 /*
  * 这里的两个if语句都将无法执行,因为已经造成了线程死锁的问题 
  * flag=1这个线程在等待flag=0这个线程把对象o2的锁解开, 
  * 而flag=0这个线程也在等待flag=1这个线程把对象o1的锁解开 
  * 然而这两个线程都不愿意解开锁住的对象,所以就造成了线程死锁的问题
  */

 /* 这是flag=0这个线程 */
 if (flag == 0) {
  synchronized (o2) {
  /* 这里先使用synchronized锁住对象o2 */
  try {
   thread.sleep(500);
  } catch (interruptedexception e) {
   e.printstacktrace();
  }
  synchronized (o1) {
   /*
   * 前面已经锁住了对象o2,只要再能锁住o1,那么就能执行打印出0的操作了 可是这里无法锁定对象o1,因为在另外一个flag=1这个线程里面已经把对象o1给锁住了 尽管锁住o1这个对象的线程会每隔500毫秒睡眠一次,可是在睡眠的时候仍然是锁住o1不放的
   */
   system.out.println("0");
  }
  }
 }
 }

 public static void main(string args[]) {
 testdeadlock td1 = new testdeadlock();
 testdeadlock td2 = new testdeadlock();
 td1.flag = 1;
 td2.flag = 0;
 thread t1 = new thread(td1);
 thread t2 = new thread(td2);
 t1.setname("线程td1");
 t2.setname("线程td2");
 t1.start();
 t2.start();
 }
}

  解决线程死锁的问题最好只锁定一个对象,不要同时锁定两个对象

生产者消费者问题:

package cn.galc.test;

/* 范例名称:生产者--消费者问题
 * 源文件名称:producerconsumer.java
 * 要 点:
 * 1. 共享数据的不一致性/临界资源的保护
 * 2. java对象锁的概念
 * 3. synchronized关键字/wait()及notify()方法
 */

public class producerconsumer {
 public static void main(string args[]){
  syncstack stack = new syncstack();
  runnable p=new producer(stack);
  runnable c = new consumer(stack);
  thread p1 = new thread(p);
  thread c1 = new thread(c);
  
  p1.start();
  c1.start();
 }
}


class syncstack{ //支持多线程同步操作的堆栈的实现
 private int index = 0;
 private char []data = new char[6]; 
 public synchronized void push(char c){
 if(index == data.length){
 try{
  this.wait();
 }catch(interruptedexception e){}
 }
 this.notify();
 data[index] = c;
 index++;
 }
 public synchronized char pop(){
 if(index ==0){
  try{
  this.wait();
  }catch(interruptedexception e){}
 }
 this.notify();
 index--;
 return data[index];
 }
}


class producer implements runnable{
 syncstack stack; 
 public producer(syncstack s){
 stack = s;
 }
 public void run(){
 for(int i=0; i<20; i++){
  char c =(char)(math.random()*26+'a');
  stack.push(c);
  system.out.println("produced:"+c);
  try{     
  thread.sleep((int)(math.random()*1000)); 
  }catch(interruptedexception e){
  }
 }
 }
}


class consumer implements runnable{
 syncstack stack; 
 public consumer(syncstack s){
 stack = s;
 }
 public void run(){
 for(int i=0;i<20;i++){
  char c = stack.pop();
  system.out.println("消费:"+c);
  try{     
  thread.sleep((int)(math.random()*1000));
  }catch(interruptedexception e){
  }
 }
 }
}

以上就是关于java线程的全部内容介绍,大家可以结合第一篇进行学习,希望可以帮助到大家。