码迷,mamicode.com
首页 > 编程语言 > 详细

多线程小知识

时间:2019-11-03 20:02:49      阅读:78      评论:0      收藏:0      [点我收藏+]

标签:div   mon   设置   ring   read   buffered   except   generate   tran   

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();
		
	}
}

  

多线程小知识

标签:div   mon   设置   ring   read   buffered   except   generate   tran   

原文地址:https://www.cnblogs.com/WINDZLY/p/11788634.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!