Haven't written a blog for a long time, there is a sense of guilt inside. One reason is that some of the things you've done recently are not suitable for public blogging.
Today, the time to say a Java multithreading in a small topic, but also the new people often encounter. The cause is that I am looking for training materials for new people, on the internet to see a lot of thread and runnable to use which of the discussion, the vast majority of such articles are the same Huang, have used the window to sell tickets examples. The following address is a more representative (funny is even runnable are misspelled):
http://www.oschina.net/question/565065_86563
It can be seen that new people find information on the Internet are some of these things, it really makes people worried AH. There are so many slots in this blog that I'm not going to spray it. Let's take the "final writing" that we think is right. Actually make up a try (I made a small change, because the original write thread and the dead loop problem, and the number of votes from 100 to 10, easy to analyze).
Class ThreadTest implements Runnable {
private int tickets = ten;
public void Run () {while
(true) {
if (Tickets > 0) {
System.out.println (Thread.CurrentThread (). GetName (
+ "is saling ticket" + tickets--);
} else {break
;
}
}}} public class ThreadDemo1 {public
static void Main (string[] args) {
threadtest t = new threadtest ();
New Thread (t). Start ();
New Thread (t). Start ();
New Thread (t). Start ();
}
}
At first glance the results are normal, with 3 windows selling 10 tickets. But more than a few times the problem came, maybe my face is relatively black, only ran 4 times to encounter a result:
Thread-0 is saling ticket 10
Thread-0 is saling ticket 8
Thread-2 is saling ticket 9
Thread-2 is saling ticket 6
Thread-1 is saling ticket 10
Thread-1 is saling ticket 4
Thread-2 is saling ticket 5
Thread-2 is saling ticket 2
Thread-2 is saling ticket 1
Thread-0 is saling ticket 7
Thread-1 is saling ticket 3
Yes, the ticket Number 10 was sold 2 times. Why this is so, believe that multithreading have knowledge of the programmer should know that--the implementation of the operator is not atomic. The solution is simple, one is to use the synchronized this built-in lock, and the second is to use atomicinteger such as concurrent package of good elements, for simplicity I use the second implementation as follows:
Import Java.util.concurrent.atomic.AtomicInteger;
public class Mythread extends Thread { //two notation public
class Mythread implements runnable{
private Atomicinteger tickets = new Atomicinteger (a);
@Override public
Void Run () {while
(true) {
int ticketnum;
if ((Ticketnum = Tickets.getanddecrement ()) > 0) {
System.out.println (Thread.CurrentThread (). GetName () + "is" Saling ticket: "
+ ticketnum);
} else {break
;
}
}} public static void Main (string[] args) {
mythread mt = new Mythread ();
thread T1 = new Thread (MT, "Win1");
Thread t2 = new Thread (MT, "Win2");
thread t3 = new Thread (MT, "Win3");
T1.start ();
T2.start ();
T3.start ();
}
The above code also illustrates a problem, this kind of posting said "Runnable can be used to share objects between multithreading, and thread can not share objects", pure nonsense, thread itself to achieve the runnable, will not lead to the difference between the object can be shared. The difference between the two biggest (or even the only one, because other differences are irrelevant to the newcomer) is that thread is a class and runnable is an interface, as with classes or interfaces, depending on the actual need for inheritance.
Finally answer the title question: which thread and runnable should be used. My advice is not to. Because Java is the language developed today, the multithreading mechanism provided at the language level has been rich and advanced, without the need to operate at the thread level at all. "Bare-thread" elements such as thread and runnable are more prone to error and require additional attention to the number of threads. Suggestions:
Simple multithreaded program that uses executor.
Simple multithreading, but do not want to focus on thread-level factors, but also familiar with the JAVA8: the use of Java8 parallel flow, it is based on the bottom of the Forkjoinpool, but also enjoy the fast function of programming.
Complex multithreaded program, using a actor library, the first push Akka.