進(jìn)程
一個(gè)靜態(tài)概念
線程
在一個(gè)時(shí)間點(diǎn)內(nèi) cpu只能執(zhí)行一個(gè)線程,分成多個(gè)時(shí)間片去分給線程占用泵额,短時(shí)間察覺不到,感覺在運(yùn)行多個(gè)線程
真正的多線程:多cpu 多內(nèi)核 確確實(shí)實(shí)是多線程
相對于進(jìn)程是動(dòng)態(tài)的
一個(gè)程序里執(zhí)行的不同路徑员寇,main是主線程
java操作線程
- java線程是通過java.lang.Thread類來實(shí)現(xiàn)的
- VM啟動(dòng)時(shí)昧穿,會(huì)有由主方法所定義的線程
- 可以通過創(chuàng)建Thread實(shí)例來創(chuàng)建
- 每個(gè)線程都是通過某個(gè)Thread對象所對應(yīng)的方法run()來完成其操作的凡壤,run()為線程體
- 通過Thread類的start()方法來啟動(dòng)一個(gè)線程
兩種方法實(shí)現(xiàn)線程
- f1
public class RunnableTest {
public static void main(String[] args) {
//啟動(dòng)線程
Runner runner = new Runner();
Thread thread = new Thread(runner);
thread.start();
loop();
}
public static void loop() {
for(int i = 0 ;i<10;i++){
System.out.println("main "+i);
}
}
}
class Runner implements Runnable{
@Override
public void run() {
// TODO Auto-generated method stub
for(int i = 0 ;i<10;i++){
System.out.println("Runner "+i);
}
}
}
/*
*
main 0
main 1
main 2
Runner 1
Runner 2
Runner 3
Runner 4
main 3
Runner 5
Runner 6
main 4
Runner 7
Runner 8
main 5
Runner 9
main 6
main 7
main 8
main 9*/
- f2
public class ThreadTest {
public static void main(String[] args) {
//啟動(dòng)線程
Runner1 runner1 = new Runner1();
runner1.start();
loop();
}
public static void loop() {
for(int i = 0 ;i<10;i++){
System.out.println("main "+i);
}
}
}
class Runner1 extends Thread{
@Override
public void run() {
// TODO Auto-generated method stub
for(int i = 0 ;i<10;i++){
System.out.println("Runner1 "+i);
}
}
}
sleep署尤、interrupt實(shí)驗(yàn)
import java.util.Date;
public class InterrputTest {
public static void main(String[] args) {
Runner3 runner3 = new Runner3();
runner3.start();
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
runner3.interrupt();
}
}
class Runner3 extends Thread{
@Override
public void run() {
// TODO Auto-generated method stub
while(true){
System.out.println(new Date());
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
System.out.println("kill");
e.printStackTrace();
return;
}
}
}
}
Wed Nov 08 21:54:05 CST 2017
Wed Nov 08 21:54:06 CST 2017
Wed Nov 08 21:54:07 CST 2017
Wed Nov 08 21:54:08 CST 2017
Wed Nov 08 21:54:09 CST 2017
Wed Nov 08 21:54:10 CST 2017
Wed Nov 08 21:54:11 CST 2017
Wed Nov 08 21:54:12 CST 2017
Wed Nov 08 21:54:13 CST 2017
Wed Nov 08 21:54:14 CST 2017
kill
java.lang.InterruptedException: sleep interrupted
at java.lang.Thread.sleep(Native Method)
at Runner3.run(InterrputTest.java:26)
join實(shí)驗(yàn)(合并線程)
import java.util.Date;
public class JoinTest {
public static void main(String[] args) {
Runner4 runner4 = new Runner4();
runner4.start();
try {
runner4.join();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
for(int i = 0 ;i < 10;i++){
System.out.println("main thread: "+new Date());
}
}
}
class Runner4 extends Thread {
public void run(){
for(int i = 0 ;i < 10;i++){
System.out.println("runner4 thread: "+new Date());
try {
sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return;
}
}
}
}
runner4 thread: Wed Nov 08 22:11:51 CST 2017
runner4 thread: Wed Nov 08 22:11:52 CST 2017
runner4 thread: Wed Nov 08 22:11:53 CST 2017
runner4 thread: Wed Nov 08 22:11:54 CST 2017
runner4 thread: Wed Nov 08 22:11:55 CST 2017
runner4 thread: Wed Nov 08 22:11:56 CST 2017
runner4 thread: Wed Nov 08 22:11:57 CST 2017
runner4 thread: Wed Nov 08 22:11:58 CST 2017
runner4 thread: Wed Nov 08 22:11:59 CST 2017
runner4 thread: Wed Nov 08 22:12:00 CST 2017
main thread: Wed Nov 08 22:12:01 CST 2017
main thread: Wed Nov 08 22:12:01 CST 2017
main thread: Wed Nov 08 22:12:01 CST 2017
main thread: Wed Nov 08 22:12:01 CST 2017
main thread: Wed Nov 08 22:12:01 CST 2017
main thread: Wed Nov 08 22:12:01 CST 2017
main thread: Wed Nov 08 22:12:01 CST 2017
main thread: Wed Nov 08 22:12:01 CST 2017
main thread: Wed Nov 08 22:12:01 CST 2017
main thread: Wed Nov 08 22:12:01 CST 2017
yield
讓出CPU,給其他線程執(zhí)行的機(jī)會(huì)
import java.util.Date;
public class YeildTest {
public static void main(String[] args) {
Runner5 first = new Runner5("First thread!");
Runner5 second = new Runner5("Second thread!");
first.start();second.start();
}
}
class Runner5 extends Thread{
Runner5(String name){
super(name);
}
public void run() {
for(int i = 0 ; i<= 20; i++){
System.out.println(getName()+" "+i);
if(i%10==0){
yield(); //切換線程
}
}
}
}
priority
線程優(yōu)先級
public class PriorityTest {
public static void main(String[] args) {
Thread aThread = new Thread(new Runner6());
Thread bThread = new Thread(new Runner7());
aThread.setPriority(Thread.NORM_PRIORITY+2);
aThread.start();bThread.start();
}
}
class Runner6 implements Runnable{
public void run() {
for(int i = 0 ; i<= 20; i++){
System.out.println("A" + i);
}
}
}
class Runner7 implements Runnable{
public void run() {
for(int i = 0 ; i<= 20; i++){
System.out.println("B" + i);
}
}
}