Java多线程
编程技术  /  houtizong 发布于 3年前   130
package test;public class Test { public static void main(String[] args) { //创建任务 PrintString print = new PrintString("jiang"); //创建任务的线程 Thread thread = new Thread(print); //调用start告诉java虚拟机线程已准备就绪 thread.start(); }}//定义任务类,实现Runnable接口,重载run方法class PrintString implements Runnable{ private String strToPrint; public PrintString(String str){ this.strToPrint = str; } public void run() { System.out.println(this.strToPrint); }}
package test;public class Test { public static void main(String[] args) { TestThread tt = new TestThread("qin"); tt.start(); }}//直接定义Thread类的扩展类,实现run方法class TestThread extends Thread{ private String strToPrint; public TestThread(String str){ this.strToPrint = str; } public void run() { System.out.println(this.strToPrint); }}
public class TestRunnable1 implements Runnable { @Override public void run() { for (int i = 1; i <= 1000; i++) { System.out.println("A : " + i); } }}public class TestRunnable2 implements Runnable { @Override public void run() { for (int i = 1; i <= 1000; i++) { System.out.println("B : " + i); } }}public class TestThead { public static void main(String[] args) { Thread t1 = new Thread(new TestRunnable1()); Thread t2 = new Thread(new TestRunnable2()); t1.setPriority(6); t2.setPriority(Thread.MAX_PRIORITY); System.out.println("A Priority : " + t1.getPriority()); System.out.println("B Priority : " + t2.getPriority()); t1.start(); t2.start(); }}
public class Test { public static void main(String[] args) { //创建一个最大线程数为3的线程执行器 ExecutorService executor1 = Executors.newFixedThreadPool(3); //为每个等待的任务创建一个新线程,所有的任务都并发的执行 ExecutorService executor2 = Executors.newCachedThreadPool(); executor1.submit(new PrintString("wang")); executor2.submit(new PrintString("mao")); executor. }}//定义任务类,实现Runnable接口,重载run方法class PrintString implements Runnable{ private String strToPrint; public PrintString(String str){ this.strToPrint = str; } public void run() { System.out.println(this.strToPrint); }}
//测试同步方法public synchronized void printStri(){ for(int i=0; i<100;i++){ System.out.println(i); }}//测试同步块,同步语句不仅可以对this对象加锁,而且可用于对任务对象加锁。public void printStri(){ synchronized(this){ for(int i=0; i<100;i++){ System.out.println(i); } }}
private static class PrintStr{ //创建一个锁 private static Lock lock = new ReentrantLock(); //测试同步方法 public void printStr(){ //加锁 lock.lock(); try{ for(int i=0; i<100;i++){ System.out.println(i); } } catch(Exception e){ e.printStackTrace(); }finally { //解锁 lock.unlock(); } }}
package test;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;import java.util.concurrent.locks.Condition;import java.util.concurrent.locks.Lock;import java.util.concurrent.locks.ReentrantLock;public class TestCondition { private static Account account = new Account(); public static void main(String[] args) { ExecutorService executorService = Executors.newFixedThreadPool(2); executorService.execute(new DepositTask()); executorService.execute(new WithdrawTask()); executorService.shutdown(); } /** * 充值任务类 */ public static class DepositTask implements Runnable { @Override public void run() { try { account.deposit((int) (Math.random() * 10) + 1); Thread.sleep(1000); } catch (InterruptedException e) { // TODO: handle exception } } } /** * 取款任务类 */ public static class WithdrawTask implements Runnable { @Override public void run() { while (true) { account.withdraw((int) (Math.random() * 10) + 1); } } } /** * 账户类 */ public static class Account { private static Lock lock = new ReentrantLock(); private static Condition newDeposit = lock.newCondition(); private int balance = 0; // 账户金额 public int getBalance() { return balance; } public void withdraw(int amount) { lock.lock(); try { while (balance < amount) { System.out.println("余额不足,等待充值"); newDeposit.await(); } balance -= amount; System.out.println("取款:" + amount + "余额:" + balance); } catch (InterruptedException e) { // TODO: handle exception } finally { lock.unlock(); } } public void deposit(int amount) { lock.lock(); try { while (balance < 10) { balance += amount; System.out.println("充值:" + amount + "余额:" + balance); newDeposit.signalAll(); } } finally { lock.unlock(); } } }}
请勿发布不友善或者负能量的内容。与人为善,比聪明更重要!
技术博客集 - 网站简介:
前后端技术:
后端基于Hyperf2.1框架开发,前端使用Bootstrap可视化布局系统生成
网站主要作用:
1.编程技术分享及讨论交流,内置聊天系统;
2.测试交流框架问题,比如:Hyperf、Laravel、TP、beego;
3.本站数据是基于大数据采集等爬虫技术为基础助力分享知识,如有侵权请发邮件到站长邮箱,站长会尽快处理;
4.站长邮箱:[email protected];
文章归档
文章标签
友情链接