进程
一个静态概念
线程
在一个时间点内 cpu只能执行一个线程,分成多个时间片去分给线程占用,短时间察觉不到,感觉在运行多个线程
真正的多线程:多cpu 多内核 确确实实是多线程
相对于进程是动态的
一个程序里执行的不同路径,main是主线程
java操作线程
- java线程是通过java.lang.Thread类来实现的
- VM启动时,会有由主方法所定义的线程
- 可以通过创建Thread实例来创建
- 每个线程都是通过某个Thread对象所对应的方法run()来完成其操作的,run()为线程体
- 通过Thread类的start()方法来启动一个线程
两种方法实现线程
- f1
public class RunnableTest {
public static void main(String[] args) {
//启动线程
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) {
//启动线程
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实验
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实验(合并线程)
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,给其他线程执行的机会
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
线程优先级
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);
}
}
}