标签:catch extends ring col 守护线程 bool err 路径 1.3
package com.xuweiwei; /** * 多线程的实现: * 由于线程是依赖进程而存在的,所以我们应该先创建一个进程出来。 * 而进程是由系统创建的,所以,我们应该去调用系统功能去创建一个进程。 * Java是不能直接带哦用系统功能的,所以,我们没有办法直接实现多线程程序。 * 但是,Java是可以调用C或C++写好的程序去实现多线程程序。 * 由C或C++去调用系统功能创建进程,然后由Java去调用这样的东西,然后提供一些API供我们使用。这样,我们就可以实现多线程程序了。 */ public class MyThread extends Thread { @Override public void run() { for (int i = 0; i < 10; i++) { System.out.println(Thread.currentThread().getName()+"= " + i); } } }
package com.xuweiwei; public class MyThreadTest { public static void main(String[] args) { MyThread t1 = new MyThread(); t1.start(); MyThread t2 = new MyThread(); t2.start(); } }
public final String getName()
public final void setName(String name)
public static Thread currentThread()
package com.xuweiwei; public class MyThread extends Thread { @Override public void run() { for (int i = 0; i < 10; i++) { System.out.println(Thread.currentThread().getName()+"= " + i); } } }
package com.xuweiwei; public class MyThreadTest { public static void main(String[] args) { MyThread t1 = new MyThread(); MyThread t2 = new MyThread(); //设置线程的名字 t1.setName("t1"); t2.setName("t2"); t1.start(); t2.start(); } }
package com.xuweiwei; public class MyThread extends Thread { public MyThread() { } public MyThread(String name) { super(name); } @Override public void run() { for (int i = 0; i < 10; i++) { System.out.println(Thread.currentThread().getName()+"= " + i); } } }
package com.xuweiwei; public class MyThreadTest { public static void main(String[] args) { MyThread t1 = new MyThread("t1"); MyThread t2 = new MyThread("t2"); t1.start(); t2.start(); } }
package com.xuweiwei; public class MyThreadTest { public static void main(String[] args) { System.out.println(Thread.currentThread().getName()); } }
public static void sleep(long millis) throws InterruptedException
public final void join() throws InterruptedException
public static void yield()
public final void setDaemon(boolean on)
public void interrupt()
package com.xuweiwei; public class MyThread extends Thread { @Override public void run() { for (int i = 0; i < 10; i++) { try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(Thread.currentThread().getName()+"= " + i); } } }
package com.xuweiwei; public class MyThread extends Thread { @Override public void run() { for (int i = 0; i < 100; i++) { System.out.println(Thread.currentThread().getName() + "= " + i); } } }
package com.xuweiwei; public class MyThreadTest { public static void main(String[] args) { MyThread t1 = new MyThread(); MyThread t2 = new MyThread(); MyThread t3 = new MyThread(); t1.setName("李渊"); t2.setName("李建成"); t3.setName("李世民"); t1.start(); try { t1.join(); } catch (InterruptedException e) { e.printStackTrace(); } t2.start(); t3.start(); } }
package com.xuweiwei; public class MyThread extends Thread { @Override public void run() { for (int i = 0; i < 100; i++) { System.out.println(Thread.currentThread().getName() + "= " + i); Thread.yield(); } } }
package com.xuweiwei; public class MyThreadTest { public static void main(String[] args) { MyThread t1 = new MyThread(); MyThread t2 = new MyThread(); MyThread t3 = new MyThread(); t1.setName("李渊"); t2.setName("李建成"); t3.setName("李世民"); t1.start(); t2.start(); t3.start(); } }
package com.xuweiwei; public class MyThread extends Thread { @Override public void run() { for (int i = 0; i < 100; i++) { System.out.println(Thread.currentThread().getName() + "= " + i); Thread.yield(); } } }
package com.xuweiwei; public class MyThreadTest { public static void main(String[] args) { MyThread t2 = new MyThread(); MyThread t3 = new MyThread(); t2.setName("李建成"); t3.setName("李世民"); t2.setDaemon(true); t3.setDaemon(true); t2.start(); t3.start(); Thread.currentThread().setName("李渊"); for (int i = 0; i < 3; i++) { System.out.println(Thread.currentThread().getName()+"= " + i); } } }
3 线程调度和线程控制
4 线程生命周期
5 死锁
6 线程间通信
7 定时器的使用
标签:catch extends ring col 守护线程 bool err 路径 1.3
原文地址:https://www.cnblogs.com/xuweiweiwoaini/p/9410609.html