Java thread entry tutorial mingjava [original]

xiaoxiao2021-03-06  145

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 implements runnable {private boolean quit = false; public void run () {while (! Quit) {// do something}} public void Quit ()}}}} If each thread is only Being it is very simple, but sometimes several threads may have access to an object and may modify it. This time you have to 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 () {if (--counter <0) {counter = 0;} return counter;}} per A 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.

Details please look at 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 ("Producter #" this.Number "Put:" i); try {sleep ((int) (Math). random () * 100));} catch (InterruptedException e) {}}}} public class CubbyHole {private int contents; private boolean available = false; public synchronized int get () {while (available == false) {try { Wait ();} catch (interruptedException e) {}} Available = false; notifyall (); returnidall ();} public synchronized void put (int value) {while {TRY {Wait ();} cat ch (InterruptedException e) {}} contents = value; available = true; notifyAll ();}} public class Consumer extends Thread {private CubbyHole cubbyhole; private int number; public Consumer (CubbyHole c, int number) {cubbyhole = c; {int value = 0; for (int = 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

Author Blog:

http://blog.9cbs.net/mingjava/

related articles

Java Thread Getting Started Tutorial Introduction The Universal Networking Frame in J2ME How to learn J2ME Use Canvas to make MIDET Welcome interface Use ANT to help develop Java projects

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

New Post(0)