⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 4.15.txt

📁 java学习的点滴
💻 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 + -