多线程 随笔记录 散乱 请不用点进来

一、join方法易错点

不要在自己的线程中开启另一个相同线程,会造成无限循环。

二、synchronized常犯的逻辑错误(下面为一个售票程序,注释部分为错误用法)

public class SellTicketDemo implements Runnable{
    Object o = new Object();
    private int ticket=50;
 @Override
 public void run() {
//  synchronized (o) {
//   while(true){
//    try {
//     Thread.sleep(100);
//    } catch (InterruptedException e) {
//     // TODO Auto-generated catch block
//     e.printStackTrace();
//    }
//    if(ticket>0){
//     System.out.println(Thread.currentThread().getName()+"正在出售第"+ticket+"张票");
//         ticket--;
//    }
//   }
//    
//  }错误,直到卖完了才释放监视器的锁
 
   while(true){
    synchronized (o) {
    try {
     Thread.sleep(100);
    } catch (InterruptedException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }
    if(ticket>0){
     System.out.println(Thread.currentThread().getName()+"正在出售第"+ticket+"张票");
         ticket--;
    }
   }
    
  }
 }
    public static void main(String[] args) {
     SellTicketDemo s = new SellTicketDemo();
  new Thread(s, "1窗口").start();
  new Thread(s, "2窗口").start();
  new Thread(s, "3窗口").start();
 }
}
三、
package Four;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.OutputStream;
public class Out {
 private int i = 1;
 private boolean flag = true;
 public synchronized void m1() throws Exception {
  while (i != 1) {// if需改成while,不然第一次判断它应该wait,第二次进来不会再判断。
   try {
    System.out.println(Thread.currentThread().getName() + i);
    this.wait();
   } catch (InterruptedException e) {
    e.printStackTrace();
   }
  }
  String str = "1";
  System.out.println(Thread.currentThread().getName() + i + "---" + str);
  if (flag) {
   OutputStream os1 = new FileOutputStream(new File("./Print/A"), true);
   os1.write(str.getBytes());
   i = 2;
   this.notifyAll();
  } else {
   OutputStream os1 = new FileOutputStream(new File("./Print/A"), true);
   os1.write(str.getBytes());
   OutputStream os2 = new FileOutputStream(new File("./Print/B"), true);
   os2.write(str.getBytes());
   OutputStream os3 = new FileOutputStream(new File("./Print/C"), true);
   os3.write(str.getBytes());
   OutputStream os4 = new FileOutputStream(new File("./Print/D"), true);
   os4.write(str.getBytes());
   i = 2;
   this.notifyAll();
  }
 }
 public synchronized void m2() throws Exception {
  while (i != 2) {
   try {
    System.out.println(Thread.currentThread().getName() + i);
    this.wait();
   } catch (InterruptedException e) {
    e.printStackTrace();
   }
  }
  String str = "2";
  System.out.println(Thread.currentThread().getName() + i + "---" + str);
  if (flag) {
   OutputStream os1 = new FileOutputStream(new File("./Print/A"), true);
   os1.write(str.getBytes());
   OutputStream os2 = new FileOutputStream(new File("./Print/B"), true);
   os2.write(str.getBytes());
   i = 3;
   this.notifyAll();
  } else {
   OutputStream os1 = new FileOutputStream(new File("./Print/A"), true);
   os1.write(str.getBytes());
   OutputStream os2 = new FileOutputStream(new File("./Print/B"), true);
   os2.write(str.getBytes());
   OutputStream os3 = new FileOutputStream(new File("./Print/C"), true);
   os3.write(str.getBytes());
   OutputStream os4 = new FileOutputStream(new File("./Print/D"), true);
   os4.write(str.getBytes());
   i = 3;
   this.notifyAll();
  }
 }
 public synchronized void m3() throws Exception {
  while (i != 3) {
   try {
    System.out.println(Thread.currentThread().getName() + i);
    this.wait();
   } catch (InterruptedException e) {
    e.printStackTrace();
   }
  }
  String str = "3";
  System.out.println(Thread.currentThread().getName() + i + "---" + str);
  if (flag) {
   OutputStream os1 = new FileOutputStream(new File("./Print/A"), true);
   os1.write(str.getBytes());
   OutputStream os2 = new FileOutputStream(new File("./Print/B"), true);
   os2.write(str.getBytes());
   OutputStream os3 = new FileOutputStream(new File("./Print/C"), true);
   os3.write(str.getBytes());
   i = 4;
   this.notifyAll();
  } else {
   OutputStream os1 = new FileOutputStream(new File("./Print/A"), true);
   os1.write(str.getBytes());
   OutputStream os2 = new FileOutputStream(new File("./Print/B"), true);
   os2.write(str.getBytes());
   OutputStream os3 = new FileOutputStream(new File("./Print/C"), true);
   os3.write(str.getBytes());
   OutputStream os4 = new FileOutputStream(new File("./Print/D"), true);
   os4.write(str.getBytes());
   i = 4;
   this.notifyAll();
  }
 }
 public synchronized void m4() throws Exception {
  while (i != 4) {
   try {
    System.out.println(Thread.currentThread().getName() + i);
    this.wait();
   } catch (InterruptedException e) {
    e.printStackTrace();
   }
  }
  String str = "4";
  System.out.println(Thread.currentThread().getName() + i + "---" + str);
  OutputStream os1 = new FileOutputStream(new File("./Print/A"), true);
  os1.write(str.getBytes());
  OutputStream os2 = new FileOutputStream(new File("./Print/B"), true);
  os2.write(str.getBytes());
  OutputStream os3 = new FileOutputStream(new File("./Print/C"), true);
  os3.write(str.getBytes());
  OutputStream os4 = new FileOutputStream(new File("./Print/D"), true);
  os4.write(str.getBytes());
  flag = false;
  i = 1;
  this.notifyAll();
 }
 public static void main(String[] args) {
  Out out = new Out();
  ThreadA a = new ThreadA(out);
  ThreadB b = new ThreadB(out);
  ThreadC c = new ThreadC(out);
  ThreadD d = new ThreadD(out);
  a.start();
  b.start();
  c.start();
  d.start();
 }
}
class ThreadA extends Thread {
 private Out writer;
 public ThreadA(Out writer) {
  super("Thread-1");
  this.writer = writer;
 }
 @Override
 public void run() {
  // TODO Auto-generated method stub
  try {
   int j = 0;
   while (j <= 10) {
    System.out.println(currentThread().getName() + "==j==" + j);
    this.writer.m1();
    j++;
   }
  } catch (Exception e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }
}
class ThreadB extends Thread {
 private Out writer;
 public ThreadB(Out writer) {
  super("Thread-2");
  this.writer = writer;
 }
 @Override
 public void run() {
  // TODO Auto-generated method stub
  try {
   int j = 0;
   while (j <= 10) {
    System.out.println(currentThread().getName() + "==j==" + j);
    this.writer.m2();
    j++;
   }
  } catch (Exception e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }
}
class ThreadC extends Thread {
 private Out writer;
 public ThreadC(Out writer) {
  super("Thread-3");
  this.writer = writer;
 }
 @Override
 public void run() {
  // TODO Auto-generated method stub
  try {
   int j = 0;
   while (j <= 10) {
    System.out.println(currentThread().getName() + "==j==" + j);
    this.writer.m3();
    j++;
   }
  } catch (Exception e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }
}
class ThreadD extends Thread {
 private Out writer;
 public ThreadD(Out writer) {
  super("Thread-4");
  this.writer = writer;
 }
 @Override
 public void run() {
  // TODO Auto-generated method stub
  try {
   int j = 0;
   while (j <= 10) {
    System.out.println(currentThread().getName() + "==j==" + j);
    this.writer.m4();
    j++;
   }
  } catch (Exception e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }
}
posted @ 2018-10-21 16:59  深海未蓝  阅读(116)  评论(0)    收藏  举报