/*
Chestnuts implement resume thread instance via runnable interface
*/
Class Dog implements runnable{
Overriding the Run function
public void Run () {
int times = 0;
while (true) {
try{
Thread.Sleep (1000);
}catch (Exception e) {
E.printstacktrace ();
}
times++;
System.out.println ("Hello, thread_world!" + times);
if (times==10) {
Break
}
}
}
}
public class Test_thread {
public static void Main (string[] args) {
Dog dog = new Dog ();
Thread t = new thread (dog);
T.start ();
}
}
Results
"C:\Program Files\java\jdk1.7.0_67\bin\java" "-javaagent:c:\program Files\jetbrains\intellij idea Community Edition 2017.2.3\lib\idea_rt.jar=6342:c:\program Files\jetbrains\intellij idea Community Edition 2017.2.3\bin "- Dfile.encoding=utf-8-classpath "C:\Program Files\java\jdk1.7.0_67\jre\lib\charsets.jar; C:\Program Files\java\jdk1.7.0_67\jre\lib\deploy.jar; C:\Program Files\java\jdk1.7.0_67\jre\lib\ext\access-bridge-64.jar; C:\Program Files\java\jdk1.7.0_67\jre\lib\ext\dnsns.jar; C:\Program Files\java\jdk1.7.0_67\jre\lib\ext\jaccess.jar; C:\Program Files\java\jdk1.7.0_67\jre\lib\ext\localedata.jar; C:\Program Files\java\jdk1.7.0_67\jre\lib\ext\sunec.jar; C:\Program Files\java\jdk1.7.0_67\jre\lib\ext\sunjce_provider.jar; C:\Program Files\java\jdk1.7.0_67\jre\lib\ext\sunmscapi.jar; C:\Program Files\java\jdk1.7.0_67\jre\lib\ext\zipfs.jar; C:\Program Files\java\jdk1.7.0_67\jre\lib\javaws.jar; C:\Program Files\java\jdk1.7.0_67\jre\lib\jce.jar; C:\Program Files\java\jdk1.7.0_67\jre\lib\jfr.jar; C:\ProGram Files\java\jdk1.7.0_67\jre\lib\jfxrt.jar; C:\Program Files\java\jdk1.7.0_67\jre\lib\jsse.jar; C:\Program Files\java\jdk1.7.0_67\jre\lib\management-agent.jar; C:\Program Files\java\jdk1.7.0_67\jre\lib\plugin.jar; C:\Program Files\java\jdk1.7.0_67\jre\lib\resources.jar; C:\Program files\java\jdk1.7.0_67\jre\lib\rt.jar;d:\zidonghua\java_test_one\target\test-classes "Test_Thread
Hello, thread_world!. 1
Hello, thread_world!. 2
Hello, thread_world!. 3
Hello, thread_world!. 4
Hello, thread_world!. 5
Hello, thread_world!. 6
Hello, thread_world!. 7
Hello, thread_world!. 8
Hello, thread_world!. 9
Hello, thread_world!. 10
Process finished with exit code 0
/*
Chestnut Multithreading Example
1. A thread executes 1+ by accepting N. +n
2. Another thread outputs Hello world! every 1 seconds
*/
Class Pig implements runnable{
int n = 0;
int times = 0;
public Pig (int n) {
THIS.N = n;
}
public void Run () {
while (true) {
try{
Thread.Sleep (1000);
}catch (Exception e) {
E.printstacktrace ();
}
times++;
System.out.println ("I am a thread that is outputting the" + Times + "A Hello world!");
if (times = = THIS.N) {
Break
}
}
}
}
Arithmetic problems
Class Bird implements runnable{
int n = 0;
int res = 0;
int times = 0;
public Bird (int n) {
THIS.N = n;
}
public void Run () {
while (true) {
try{
Thread.Sleep (1000);
}catch (Exception e) {
E.printstacktrace ();
}
Res + = (++times);
System.out.println ("The current result is:" + res);
if (times = = THIS.N) {
System.out.println ("The final result is:" + res);
Break
}
}
}
}
public class Test_thread {
public static void Main (string[] args) {
Pig pig = new Pig (5);
Bird Bird = new Bird (5);
thread T1 = new Thread (pig);
Thread t2 = new Thread (bird);
T1.start ();
T2.start ();
}
}
Results
I am a thread that is outputting a 1th hello world!
The current result is: 1
I am a thread that is outputting a 2nd Hello world!
The current result is: 3
I am a thread that is outputting a 3rd Hello world!
The current result is: 6
I am a thread that is outputting a 4th Hello world!
The current result is: 10
I am a thread that is outputting a 5th Hello world!
The current result is: 15
Finally the result is: 15
/*
Chestnut deep understanding thread objects can only start a thread regardless of inheriting thread or implementing the Runnable interface cannot use start to start the same thread 2 times
*/
Class Cat extends thread{
public void Run () {
System.out.println ("11");
}
}
Class Dog implements runnable{
public void Run () {
System.out.println ("2");
}
}
public class Test_thread {
public static void Main (string[] args) {
Cat CAT1 = new Cat ();
Cat1.start ();
Dog dog1 = new Dog ();
Thread t = new thread (DOG1);
T.start ();
T.start ();
}
}
Results
Exception in thread "main" 11
2
Java.lang.IllegalThreadStateException
At Java.lang.Thread.start (thread.java:705)
At Test_thread.main (test_thread.java:26)
The difference between two ways to create a thread
There are two ways to create a thread:1InheritedThread;2RealizeRunnablePick up?;What is the difference between these two kinds of laws?
? implementRunnableCharacteristics of the pick-up
1,? ImplementRunnableThe method of creating objects can avoidJavaThe limitations brought about by the single-inheritance mechanism;
2,? ImplementRunnableYou can implement multiple threads to share the same segment code(Data);
So suggest home If your program has synchronization logic requirements, then make?Runnablemethod to create a thread.
Java thread Synchronization -- Ask a question
Multi-threaded concurrency brings many benefits to our programming and completes more efficient programs. But it also brings us
Thread safety issues.
synchronization of Java threads- - solving problems
The key to solving the problem is to ensure the original nature of the code that is prone to problems , the so-called original sex refers to: When a thread in
When you take a piece of code, the other thread must wait until the a thread has finished, before it can hold the code. Also
A line up, a solution.
Java processing thread Two-step? Often simple, just need to synchronize the code snippet,?:
Synchronized (Object) { The code you want to synchronize }
JavaSynchronization of Threads--Solve the problem
Explanation of the synchronization mechanism:
JavaAny type of object has a flag bit, and the flag bit has0、1Two states with a starting state of1,
When a thread has aSynchronized (Object)Statement,ObjectThe object's flag bit becomes0The state, until
Finish the wholeSynchronizedAfter the code block in the statement, the object's flag bit? Back to1State.
When a thread is in theSynchronized (Object)Statement, first check theObjectThe object's flag bit, as
Fruit is0Status indicating that another thread is already in the hold?SynchronizedInclude the code, then this thread will temporarily
When blocked, let outCpuResource until another thread finishes the associated synchronization code and flags the object
Bit becomes state, the blocking of this thread is canceled and the thread can continue to be shipped? Change the flag of Object
is 0 states, anti-other threads are re-entered in the associated synchronization code block.
If there are multiple threads waiting for the same object's flag bit?? When the object's flag bit is in a blocked state, the
When the 1 state is reached, only one thread can enter the synchronization code, and the other threads are still in a blocked state.
Special Note:
1, the above-mentioned sign, the term is the object lock ,lock. The database will have locks, table locks, etc.
2, synchronized (object)//object ( that is, Objects lock ) can be any type of object
"Java" "Thread"