public class TestThread { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub //1 线程对象 Thread thread1 = new MThread(1); //2 线程开启 thread1.start();//run() //1 线程对象 Thread thread2 = new MThread(2); //2 线程开启 thread2.start(); }}class MThread extends Thread{ int num = 0; public MThread(int num) { this.num = num; } @Override public void run() { // TODO Auto-generated method stub for(int i=0;i<100;i++) System.out.print(num + " "); }}
public class TestRunnable { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub //1 线程对象 Thread thread1 = new Thread(new Print(1)); thread1.start(); Thread thread2 = new Thread(new Print(2)); thread2.start(); }}class Print implements Runnable{ private int num = 0; public Print(int num) { this.num = num; } public void run() { for(int i=0;i<100;i++) System.out.print(num + " "); }}
public class TestSellTicket { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub SellTickets sellTickets = new SellTickets(10); Thread thread1 = new Thread(sellTickets); thread1.start(); Thread thread2 = new Thread(sellTickets); thread2.start(); }}class SellTickets implements Runnable{ private int tickets = 0; public SellTickets(int tickets) { this.tickets = tickets; } @Override public void run() { // TODO Auto-generated method stub for(int i=0;i<10;i++) { if(tickets > 0) System.out.println(Thread.currentThread().getName() + "卖掉了" + (tickets--) + "号票"); } }}
public class TestShared { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub //1 线程对象 Thread thread1 = new MyThread(10); //2 线程开启 thread1.start();//run() //1 线程对象 Thread thread2 = new MyThread(10); //2 线程开启 thread2.start(); }}class MyThread extends Thread{ private static int tickets = 10; public MyThread(int tickets) { //this.tickets = tickets; } @Override public void run() { // TODO Auto-generated method stub for(int i=0;i<10;i++) { if(tickets > 0) System.out.println(Thread.currentThread().getName() + "卖掉了" + (tickets--) + "号票"); } }}
public class TestSync { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Account account = new Account(); Thread thread1 = new Thread(new Withdraw(account), "卡"); Thread thread2 = new Thread(new Withdraw(account), "存折"); thread1.start(); thread2.start(); }}//账户class Account{ private int balance = 10000;//余额 //取款 public synchronized void withdraw(int count) { //同步代码块 //synchronized (this) //{ if(balance >= count) { System.out.println(Thread.currentThread().getName() + "成功取款:" + count); balance -= count; System.out.println("余额:" + balance); } else { System.out.println("余额不足"); } //} }}class Withdraw implements Runnable{ private Account acount; public Withdraw(Account account) { // TODO Auto-generated constructor stub this.acount = account; } @Override public void run() { // TODO Auto-generated method stub acount.withdraw(8000); }}
import java.util.LinkedList;public class TestProducerConsumer { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Warehouse warehouse = new Warehouse(10);//仓库 Thread thread1 = new Thread(new Producer(warehouse));//生产者线程 Thread thread2 = new Thread(new Consumer(warehouse));//消费者线程 thread1.start(); thread2.start(); }}//仓库class Warehouse{ private LinkedListlinkedList = new LinkedList ();//存放产品 private final int MAX;//仓库最大容量 private int id = 0; public Warehouse(int max) { // TODO Auto-generated constructor stub MAX = max; } //添加一个产品 public synchronized void add() { if(linkedList.size() == MAX) { try { System.out.println("满了,等待消费者唤醒"); this.wait(); } catch (InterruptedException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } String str = "产品" + id++; linkedList.add(str); System.out.println("生产了:" + str); notify(); } //取出一个产品 public synchronized void sub() { if(linkedList.size() == 0) { try { System.out.println("空了,等待生产者唤醒"); wait(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } try { Thread.sleep(100); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println("消费了:" + linkedList.getFirst()); linkedList.remove(); this.notify();//只会把在this上等待的线程唤醒一个 //this.notifyAll();//只会把在this上等待的线程全部唤醒 }}//生产者class Producer implements Runnable{ private Warehouse warehouse;//仓库 public Producer(Warehouse warehouse) { // TODO Auto-generated constructor stub this.warehouse = warehouse; } @Override public void run() { // TODO Auto-generated method stub while(true) { warehouse.add(); } }}//生产者class Consumer implements Runnable{ private Warehouse warehouse;//仓库 public Consumer(Warehouse warehouse) { // TODO Auto-generated constructor stub this.warehouse = warehouse; } @Override public void run() { // TODO Auto-generated method stub while(true) { warehouse.sub(); } }}
public class TestInterupt { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Thread thread = new NewMyThread(); thread.start(); thread.interrupt(); }}class NewMyThread extends Thread{ @Override public void run() { // TODO Auto-generated method stub //System.out.println("线程要睡眠10秒"); synchronized (this) { System.out.println("线程要wait"); try { wait(3000); System.out.println("超时"); } catch (InterruptedException e) { // TODO Auto-generated catch block System.out.println("被打断了"); } } }}
public class TestJoin { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Thread thread = new NMThread(Thread.currentThread()); thread.start(); //thread.interrupt(); //主线程要等待子线程的结束 try { thread.join(); } catch (InterruptedException e) { // TODO Auto-generated catch block System.out.println("主线程的等待被打断"); } System.out.println("主线程结束"); } }class NMThread extends Thread{ private Thread mainThread; public NMThread(Thread mainThread) { // TODO Auto-generated constructor stub this.mainThread = mainThread; } @Override public void run() { // TODO Auto-generated method stub //System.out.println("线程要睡眠10秒"); for(int i=0;i<100;i++) { if(i==50) mainThread.interrupt(); System.out.println(i); } System.out.println("子线程结束"); }}
public class TestDeadlock { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Ch cha = new Ch("A"); Ch chb = new Ch("B"); Thread thread1 = new Thread(new Eat1(cha,chb)); Thread thread2 = new Thread(new Eat2(cha,chb)); thread1.start(); thread2.start(); }}//筷子class Ch{ private String name; public Ch(String name) { // TODO Auto-generated constructor stub this.name = name; }}class Eat1 implements Runnable{ private Ch a; private Ch b; public Eat1(Ch a,Ch b) { // TODO Auto-generated constructor stub this.a = a; this.b = b; } @Override public void run() { // TODO Auto-generated method stub while(true) synchronized (a) { System.out.println("哲学家1获得A筷子"); synchronized (b) { System.out.println("哲学家1获得B筷子"); System.out.println("哲学家1吃一口"); } } }}class Eat2 implements Runnable{ private Ch a; private Ch b; public Eat2(Ch a,Ch b) { // TODO Auto-generated constructor stub this.a = a; this.b = b; } @Override public void run() { // TODO Auto-generated method stub while(true) synchronized (a) { System.out.println("哲学家2获得A筷子"); synchronized (b) { System.out.println("哲学家2获得B筷子"); System.out.println("哲学家2吃一口"); } } }}