Java learning --- thread

Source: Internet
Author: User

1. inherit the Thread class and implement the run Method

class TestThread{public static void main(String[] args){Thread1 t1=new Thread1();t1.start();int index=0;while(true){if(index++==500){t1.stopThread();t1.interrupt();break;}System.out.println(Thread.currentThread().getName());}System.out.println("main() exit");}}class Thread1 extends Thread{private boolean bStop=false;public synchronized void run(){while(!bStop){try{wait();}catch(InterruptedException e){//e.printStackTrace();if(bStop)return;}System.out.println(getName());}}public void stopThread(){bStop=true;}}

========================================================== ======================================

2. Implement the Runable interface.

class TicketsSystem{public static void main(String[] args){SellThread st=new SellThread();new Thread(st).start();try{Thread.sleep(1);}catch(Exception e){e.printStackTrace();}st.b=true;new Thread(st).start();//new Thread(st).start();//new Thread(st).start();}}class SellThread implements Runnable{int tickets=100;Object obj=new Object();boolean b=false;public void run(){if(b==false){while(true)sell();}else{while(true){synchronized(obj){try{Thread.sleep(10);}catch(Exception e){e.printStackTrace();}synchronized(this){if(tickets>0){System.out.println("obj:"+Thread.currentThread().getName()+" sell tickets:"+tickets);tickets--;}}}}}}public synchronized void sell(){synchronized(obj){if(tickets>0){try{Thread.sleep(10);}catch(Exception e){e.printStackTrace();}System.out.println("sell():"+Thread.currentThread().getName()+" sell tickets:"+tickets);tickets--;}}}}

========================================================== ========================================================== ====================

3. Use internal classes to implement threads

class MultiThread{public static void main(String[] args){MyThread mt=new MyThread();/*new Thread(mt).start();new Thread(mt).start();new Thread(mt).start();new Thread(mt).start();*/mt.getThread().start();mt.getThread().start();mt.getThread().start();mt.getThread().start();//mt.setDaemon(true);//mt.setPriority(Thread.MAX_PRIORITY);//mt.start();int index=0;while(true){/*if(index++==1000)break;*/System.out.println("main:"+Thread.currentThread().getName());}}}class MyThread //implements Runnable//extends Thread{int index=0;private class InnerThread extends Thread{public void run(){while(true){System.out.println(Thread.currentThread().getName()+":"+index++);}}}Thread getThread(){return new InnerThread();}/*public void run(){while(true){System.out.println(Thread.currentThread().getName()+":"+index++);//yield();}}*/}

========================================================== ==========================================

4. synchronous code block synchronized

Package cn. sunzn. synchronize; public class SynchronizeCode {public static void main (String [] args) {new Thread () {public void run () {while (true) {System. out. println ("synchronization code ");}};}. start (); new Thread () {public void run () {while (true) {System. out. println ("SynchronizeCode ");}};}. start ();}}

========================================================== ========================================================== =

5. wait and consumer Y in the thread-implement the producer and consumer Design Mode

class Test{public static void main(String[] args){Queue q=new Queue();Producer p=new Producer(q);Consumer c=new Consumer(q);p.start();c.start();}}class Producer extends Thread{Queue q;Producer(Queue q){this.q=q;}public void run(){for(int i=0;i<10;i++){q.put(i);System.out.println("Producer put "+i);}}}class Consumer extends Thread{Queue q;Consumer(Queue q){this.q=q;}public void run(){while(true){System.out.println("Consumer get "+q.get());}}}class Queue{int value;boolean bFull=false;public synchronized void put(int i){if(!bFull){value=i;bFull=true;notify();}try{wait();}catch(Exception e){e.printStackTrace();}}public synchronized int get(){if(!bFull){try{wait();}catch(Exception e){e.printStackTrace();}}bFull=false;notify();return value;}}





Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.