?? 4.15.txt
字號:
一.線程的創建和調用
概念:
進程:正在CPU去執行的程序.
線程:等待CPU去執行的程序.
實現線程的兩種方法:
1.繼承Thread類
public class ThreadDemo extends Thread{
public void run()
{
int i=0;
while(true)
{
System.out.println("loop1:"+i);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO 自動生成 catch 塊
e.printStackTrace();
}
i++;
}
}
}
-----------------------------------------------------
public class ThreadDemo1 extends Thread{
public void run()
{
int i=0;
while (true)
{
System.out.println("loop2:"+i);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO 自動生成 catch 塊
e.printStackTrace();
}
i++;
}
}
2.繼承Runnable接口
public class RunnableDemo implements Runnable{
public void run()
{
int i=0;
while (true)
{
System.out.println("Runnable is running---------");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO 自動生成 catch 塊
e.printStackTrace();
}
i++;
}
}
}
3.測試類
public class Test {
public static void main(String[] args) {
ThreadDemo t=new ThreadDemo();
t.start();
ThreadDemo1 t1=new ThreadDemo1();
t1.start();
RunnableDemo r1=new RunnableDemo();
Thread t2=new Thread(r1);
t2.start();
}
}
二.線程的停止
public class StopDemo implements Runnable{
public boolean flg=true;
public void run(){
int i=0;
while(flg==true)
{
System.out.println("線程要停止了");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO 自動生成 catch 塊
e.printStackTrace();
}
i++;
}
}
public void shutDown()
{
flg=false;
}
}
---------------------------------------------------
public class TestStop {
public static void main(String[] args) {
StopDemo s1=new StopDemo();
Thread t1=new Thread(s1);
t1.start();
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
// TODO 自動生成 catch 塊
e.printStackTrace();
}
s1.shutDown();
}
}
三.Thread類中的方法join(),作用是使調用join方法的線程執行完,才能執行其它的線程。
public class JoinDemo {
int[] a = new int[20];
public static void main(String [] args)
{
JoinDemo j1=new JoinDemo();
j1.print();
}
public void print()
{
T1 t1=new T1();
Thread t=new Thread(t1);
t.start();
try {
t.join();
} catch (InterruptedException e) {
// TODO 自動生成 catch 塊
e.printStackTrace();
}
for (int i=0;i<20;i++)
{
System.out.println("a["+i+"]="+a[i]);
}
}
class T1 implements Runnable
{
public void run()
{
for(int i=0;i<20;i++)
{
a[i]=i-30;
}
}
}
}
四.Thread類中的方法setPriority(int i),作用是設置線程的優先級,i的值越大,優先級越高.
public class TreadDemo1 implements Runnable {
public void run()
{
int i=0;
while (true)
{
System.out.println("loop1:"+i);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO 自動生成 catch 塊
e.printStackTrace();
}
i++;
}
}
}
--------------------------------------------------
public class ThreadDemo2 implements Runnable{
public void run()
{
int i=0;
while (true)
{
System.out.println("loop2:"+i);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO 自動生成 catch 塊
e.printStackTrace();
}
i++;
}
}
}
------------------------------------------------------
測試類
public class TestYouxian {
public static void main(String[] args) {
TreadDemo1 t1=new TreadDemo1();
Thread tt=new Thread(t1);
tt.setPriority(1);
tt.start();
ThreadDemo2 t2=new ThreadDemo2();
Thread tt1=new Thread(t2);
tt1.setPriority(10);
tt1.start();
}
}
五.Thread類中的方法daemon(true),作用是當主線程結束時,調用此方法的線程也隨之結束。
public class DaemonThreadDemo implements Runnable {
private boolean flag = true;
public void run() {
int i = 0;
while (flag == true) {
System.out.println("daemon: " + i++);
try {
Thread.sleep(1000);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
---------------------------------------------------------
測試類:
public class Test {
public static void main(String[] args) {
DaemonThreadDemo r = new DaemonThreadDemo();
Thread t = new Thread(r);
t.setDaemon(true);
t.start();
try {
Thread.sleep(10000);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("Thread main is over");
/**
* 主線程結束后,daemom線程也隨之結束
*/
}
}
六.底層網絡連接的例子(看JAVA教學例子)
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -