Java thread entry tutorial

xiaoxiao2021-03-06  151

Threads are Java's embedded features, threads are not easy to master, there are books that specifically introduce Java threads, readers can refer to. This shows the importance of Java threads, this article will detail the basic knowledge of threads.

Sometimes you may want to write a program, perform related tasks every other time, this time you can use Timer and Timertask, it is very convenient. You can refer to http://blog.9cbs.neet/mingjava/archive/2004/07/04/33749.aspx.

There are two ways to implement a thread in Java, and the first is to implement the RunNable interface implementation it () method, the second is to inherit the Thread class, override its Run () method. The following is sample code: public class DoSomething implements Runnable {public void run () {// here is where you do something}} public class DoAnotherThing extends Thread {public void run () {// here is where you do something}} This The difference between the two methods is that if your class has inherited other classes, then you can only choose the runnable interface, because Java only allows for inheritance.

There are four states in Java, respectively, running, ready, hang, and end. If a thread is over, it will explain that he is a dead thread. When you call a start () of a thread instance, this time the thread enters ready state, pay attention is not a running state, when the virtual machine begins to assign the run time of the CPU, the thread begins to enter the running state, when the thread When entering the waiting state, for example, when an event occurs, this is at this time.

Start a thread You only need to call the start () method, there are two ways to start threads for two ways to implement threads, as follows: dosomething DOIT = New Dosomething (); thread mythread = new thread (DOIT); mythread.start (); DoanothersHing DOIT = New DoanoNotherTHING (); DOIT.Start (); Due to the STOP () method in Thread () method due to security is not recommended, if you want to stop a thread, you can set a semaphore , For example: public class mythread imports runnable {private boolean quit = false;

Public void run () {while (! quit) {// do something}}

Public void Quit () {quit = true;}} If each thread only does itself, it is very simple, but sometimes several threads may access an object at the same time and may modify it. You must use the thread synchronization in the method or code block using the keyword synchronized, for example: public class counter {private int counter;

Public synchronized int increment () {Return counter;}

Public synchronized int decrement () {ix (--counter <0) {counter = 0;}

Return Counter;} Every Java object can be the most monitors. When the thread accesss its SYNCHRONIZED method, he only allows only one thread to visit him at a time, so that other thread line queues wait. This avoids multi-threading to damage the shared data. Remember Synchronized is a system resource to reduce program execution efficiency, so it is necessary to use it when you need synchronization, especially in the development of J2ME. If you want the thread to wait for an event, then continue to execute, then this involves the scheduling of threads. Implemented in Java through Wait (), Notify (), NotifyAll (), which is defined in the Object class. When you want to call the obj.wait () method when thread hangs, the same The OBJ is called NOTIFY () to restart the thread. Finally, the article ends this article with an example provided by Sun, and the content is Producer to generate a number and consumer consumption, this applet basically covers all the knowledge points of this article. Please study the code PUBLIC CLASS Producer Extends Thread {Private CubbyHole CubbyHole; Private Int Number;

Public Producer (cubbyhole c, int number {cubbyhole = C; this.number = number;}

Public void Run () {for (int i = 0; i <10; i ) {cubbyhole.put (i); system.out.println ("Producer #" this.Number "Put:" i); Try {SLEEP ((int) (math.random () * 100);} catch (interruptedException e) {}}}}

Public class cubbyhole {private int contents; private bolean available = false;

Public synchronized int GET () {while (available == false) {Try {Wait ();} catch (interruptedException e) {}} Available = false; notifyall (); Return Contents;

Public synchronized void put (int value) {while {TRY {Wait ();} catch (interruptedException e) {}}}}}}}}}}}}}}}} = true; notifyall ();}

Public class consumer extends thread {private cubbyhole cubbyhole; private int number; public consumer (cubbyhole c, int number) {cubbyhole = C; this.number = number;}

Public void Run () {int value = 0; for (int i = 0; i <10; i ) {value = cubbyhole.get (); system.out.println ("consumer #" this.number "got : " value);}}}

Public class producerconsumertest {

Public static void main (String [] args) {

Cubbyhole c = new cubbyhole ();

Producter P1 = New Producter (C, 1);

Consumer C1 = New Consumer (C, 1);

p1.start ();

C1.Start ();

}

}

Sun said that the result of the output should be in the following form, but in my machine is not the case, do some changes are correct, interested friends can run the results, welcome to discuss and discuss! Producer # 1 put: 0Producer # 1 put: 1consumer # 1 Got: 1Producer # 1 put: 2consumer # 1 Got: 2Producer # 1 put: 3consumer # 1 Got: 3Producer # 1 put: 4consumer # 1 got: 4Producer # 1 Got: 5Producer # 1 put: 6consumer # 1 Got: 6Producer # 1 put: 7consumer # 1 Got: 7Producer # 1 put: 8consumer # 1 Got: 8Producer # 1 put: 9Consumer # 1 got: 9

转载请注明原文地址:https://www.9cbs.com/read-128454.html

New Post(0)