亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

對于多線程的開啟的疑問

對于多線程的開啟的疑問

月亮島Superman 2018-08-03 15:20:26
代碼如下:疑問:對于方式一和方式二? 區別就是在new Thread()時候,方式一是傳遞了同一個 Processor對象p,而方式二是new了兩個Processor對象,請問單獨從這點來說 對于后續 啟動兩個線程有什么不同之處嗎?即使new Thread是傳遞同一個Processor對象p? t1.start() 和 t2.start() 都會開啟兩個線程。而這種方式 也是 會開啟兩個線程? 這兩種方式有什么區別嗎????Thread t1 = new Thread(new Processor());?Thread t2 = new Thread(new Processor());public class synchronized {? ? public static void main(String[] args) throws InterruptedException { //方式一 MyClass m = new MyClass(); Processor p = new Processsor(m); Thread t1 = new Thread(p); Thread t2 = new Thread(p);?t1.start();?t2.start(); //方式二----------------------------------------------------------------- ? ? ? ? Thread t1 = new Thread(new Processor());? ? ? ? Thread t2 = new Thread(new Processor());? ? ? ? t1.start();? ? ? ? t2.start();? ? }}class Processsor implements Runnable { MyClass mc; public Processor() {} public Processor(MyClass mc) { this.mc? =? mc; }? ? @Override? ? public void run() {? ? ? ? if ("t1".equals(Thread.currentThread().getName())) {? ? ? ? ? ? MyClass02.m1();? ?//類鎖? ? ? ? }? ? ? ? if ("t2".equals(Thread.currentThread().getName())) {? ? ? ? ? ? MyClass02.m2();? ? ? ? }? ? }}class MyClass {? ? public synchronized static void m1() {? ? ? ? try {? ? ? ? ? ? Thread.sleep(5000);? ? ? ? } catch (InterruptedException e) {? ? ? ? ? ? e.printStackTrace();? ? ? ? }? ? ? ? System.out.println("m1........");? ? }??? ? public synchronized static void m2() {? ? ? ? System.out.println("m2........");? ? }}
查看完整描述

1 回答

?
anet

TA貢獻79條經驗 獲得超19個贊

沒有區別。

查看完整回答
反對 回復 2018-08-18
  • 1 回答
  • 0 關注
  • 1099 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號