Learn the thread, harvest a lot, record it.
One, the main two methods of implementation of the thread.
1. Inherit the thread class, overriding the run () method
The main method creates subclasses, references call the start () method
Examples are as follows:
Inherit the thread class, overriding the run () method
public class Threadone extends Thread {
public void Run () {
for (int i = 1; I <=100; i++) {
System.out.println (This.getname () + ":" +i);
}
}
public static void Main (string[] args) {
Create a Thread object
Threadone Threadone = new Threadone ();
Call Start ()
Threadone.start ();
}
}
2. Implement the runnable () interface to implement the run () method.
In the Mian method instance of the class, the instance thread class and the reference thread that holds the class call Start ().
Instance:
Implement the runnable () interface to implement the run () method
public class MyThread implements Runnable {
public void Run () {
for (int i=0; i<10; i++) {
System.out.println (Thread.CurrentThread (). GetName () + "put an apple:" +i);
try {
Thread.Sleep (1000);
} catch (Interruptedexception e) {
throw new RuntimeException (e);
}
}
}
public static void Main (string[] args) {
Creating the Mythread Class
MyThread MyThread =new MyThread ();
Create thread, and hold Mythread's app
Thread thread = new Thread (myThread);
Call Start ()
Thread.Start ();
}
}
Two. Regular alternating thread topics
For example, to implement two threads that can alternately print a time every 1 seconds, each thread prints 5 times after the end thread
Result requirements:
Thread-0 Sun Feb 11:21:52 CST 2016;
Thread-1 Sun Feb 11:21:53 CST 2016
Thread-0 Sun Feb 11:21:54 CST 2016
Thread-1 Sun Feb 11:21:55 CST 2016
Thread-0 Sun Feb 11:21:56 CST 2016
Thread-1 Sun Feb 11:21:57 CST 2016
Thread-0 Sun Feb 11:21:58 CST 2016
Thread-1 Sun Feb 11:21:59 CST 2016
Thread-0 Sun Feb 11:22:00 CST 2016
Thread-1 Sun Feb 11:22:01 CST 2016
Problem Analysis:
The first is to implement 2 threads: The function is the creation time, the second need to be executed alternately, must lock the synchronized; these are all understandable.
The important thing is how to control this lock, with what control, the method is: Define a static state value, the value is 1, 2;
When state=1, thread 1 is executed, thread 2 waits, execution ends state=2, and Notifyall () or Notify () method is called;
When state=2, thread 2 is executed, thread 1 waits, execution ends state=1, and Notifyall () or Notify () method is called;
Finally left control 5 times the problem, the respective definition 2 static int variable using while control Bai, each thread executes once, corresponding variable value plus 1 to 5 end
Code:
public class threaddate{
State values control which program is executed with
private static int state = 1;
Control the number of times a thread executes
private static int num1 = 1;
Number of threads that control thread 2 execution
private static int num2 = 1;
public static void Main (string[] args) {
Use this class as a lock
Final threaddate t=new threaddate ();
Create the first thread and call the start () method
New Thread (New Runnable () {
public void Run () {
Controlling execution times with while NUM1
while (num1<=5) {
Locking
Synchronized (t) {
The state value should be the first to determine whether the execution
if (state!=1) {
try {
T.wait ();
} catch (Interruptedexception e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
System.out.println (Thread.CurrentThread (). GetName () + "" +new Date ());
try {
Thread.Sleep (1000);
} catch (Interruptedexception e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
Execution end, state value changed
state = 2;
T.notifyall ();
}
num1++;
}
}
}). Start ();;
Create a second thread and call the start () method
New Thread (New Runnable () {
public void Run () {
while (num2<=5) {
Synchronized (t) {
if (state!=2) {
try {
T.wait ();
} catch (Interruptedexception e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
System.out.println (Thread.CurrentThread (). GetName () + "" +new Date ());
try {
Thread.Sleep (1000);
} catch (Interruptedexception e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
state = 1;
T.notifyall ();
}
num2++;
}
}
}). Start ();
}
}
2 implementations of threads and alternate execution of threads