多线程小知识
Posted windzly
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了多线程小知识相关的知识,希望对你有一定的参考价值。
package test; import java.awt.List; import java.awt.image.AreaAveragingScaleFilter; import java.lang.reflect.Array; import java.math.BigDecimal; import java.math.BigInteger; import java.util.*; import java.util.concurrent.CountDownLatch; import java.util.concurrent.locks.Condition; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; import java.io.BufferedInputStream; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.io.Reader; import java.io.Writer; import javax.security.auth.callback.LanguageCallback; import javax.swing.text.StyledEditorKit.BoldAction; class Re implements Runnable { @Override public void run() { // TODO Auto-generated method stub for(int i = 1; i <= 50; i ++) { System.out.println(Thread.currentThread().toString() + "........" + i); } } } public class Main { public static void main(String[] args) throws Exception { Scanner scanner = new Scanner(new BufferedInputStream(System.in)); PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out)); new Thread(new Runnable() { @Override public void run() { System.out.println(11); } }) { public void run() { for(int i = 1; i <= 50; i ++) { System.out.println(Thread.currentThread().getName() + " ......."); } } }.start(); } } 我们在一个线程里面先运行子任务,若是没有子任务就运行自己的任务,自己要是没有任务就运行thread 我们在线程里面t1.setDaemon(true);我们可以设置后台任务,后台任务的好处就是在前台任务结束之后java虚拟机就会退出 我们还可以t1.yield();让这个线程释放执行权 t1.setPriority(Thread.MAX_PRIORITY);这个我们可以让线程的优先级变高 cpu适当照顾一下线程 t1.interrupt();打断线程,这个东西我们需要抛出异常 package test; import java.awt.List; import java.awt.image.AreaAveragingScaleFilter; import java.lang.reflect.Array; import java.math.BigDecimal; import java.math.BigInteger; import java.util.*; import java.util.concurrent.CountDownLatch; import java.util.concurrent.locks.Condition; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; import java.io.BufferedInputStream; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.io.Reader; import java.io.Writer; import javax.security.auth.callback.LanguageCallback; import javax.swing.text.StyledEditorKit.BoldAction; class Re implements Runnable { @Override public void run() { // TODO Auto-generated method stub for(int i = 1; i <= 50; i ++) { System.out.println(Thread.currentThread().toString() + "........" + i); } } } public class Main { public static void main(String[] args) throws Exception { Scanner scanner = new Scanner(new BufferedInputStream(System.in)); PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out)); new Thread(new Runnable() { @Override public void run() { System.out.println(11); } }) { public void run() { for(int i = 1; i <= 50; i ++) { System.out.println(Thread.currentThread().getName() + " ......."); } } }.start(); Thread t1 = new Thread(); t1.setDaemon(true); t1.yield(); t1.setPriority(Thread.MAX_PRIORITY); t1.interrupt(); } }
以上是关于多线程小知识的主要内容,如果未能解决你的问题,请参考以下文章