第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

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

Java同步死鎖?

Java同步死鎖?

慕慕森 2021-07-20 20:16:16
我對 Java 并發(fā)非常陌生,并且在嘗試使用鎖和監(jiān)視器編寫玩具問題時陷入困境。問題的要點是,我有一個具有g(shù)et和put方法的類,并且本質(zhì)上是線程消費和生產(chǎn)的容器。對于我的生活,我無法正確同步,最終會出現(xiàn)死鎖或IllegalMonitorStateException.package concurrencyobject ThreadsMain extends App {  val syncVar = new SyncVar[Int]()  val producer = new Thread {    override def run(): Unit = {      for (x <- 1 to 15) {        syncVar.synchronized {          if (!syncVar.isEmpty) {            syncVar.wait()          } else {            syncVar.put(x)            syncVar.notify()          }        }      }    }  }  producer.run()  val consumer = new Thread {    this.setDaemon(true)    override def run(): Unit = {      while (true) {        syncVar.synchronized {          if (syncVar.isEmpty) {            syncVar.wait()          } else {            println(syncVar.get())            syncVar.notify()          }        }      }    }  }  consumer.run()  producer.join()  consumer.join()}class SyncVar[T]() {  var isEmpty: Boolean = true  var value: Option[T] = None  def get(): T = {    if (isEmpty) throw new Exception("Get from empty SyncVar")    else {      val toReturn = value.get      value = None      isEmpty = true      toReturn    }  }  def put(x: T): Unit = {    if (!isEmpty) throw new Exception("Put on non-empty SyncVar")    else {      value = Some(x)      isEmpty = false    }  }}
查看完整描述

1 回答

?
隔江千里

TA貢獻1906條經(jīng)驗 獲得超10個贊

有幾個問題:

  1. 你應(yīng)該使用starton not run

  2. 如果您正在使用join,則將踏面設(shè)置為守護線程是沒有意義的。

  3. 當(dāng)您if ... else在生產(chǎn)商中進行操作時,您只會得到奇數(shù)。它應(yīng)該只是if和其余的if(實際上while是一個更好的做法)。

我認為這樣代碼可以滿足您的需求:

object ThreadsMain extends App {

  val syncVar = new SyncVar[Int]()

  val isDone = new AtomicBoolean(false)


  val producer = new Thread {

    override def run(): Unit = {

      for (x <- 1 to 15) {

        syncVar.synchronized {

          while (!syncVar.isEmpty) {

            syncVar.wait()

          }

          syncVar.put(x)

          syncVar.notify()

        }

      }

      isDone.set(true)

    }

  }


  producer.start()


  val consumer = new Thread {


    override def run(): Unit = {

      while (!isDone.get()) {

        syncVar.synchronized {

          while (syncVar.isEmpty) {

            syncVar.wait()

          }

          println(syncVar.get())

          syncVar.notify()

        }

      }

    }

  }


  consumer.start()


  producer.join()


  consumer.join()

}


class SyncVar[T]() {

  var isEmpty: Boolean = true

  var value: Option[T] = None


  def get(): T = {

    if (isEmpty) throw new Exception("Get from empty SyncVar")

    else {

      val toReturn = value.get

      value = None

      isEmpty = true

      toReturn

    }

  }


  def put(x: T): Unit = {

    if (!isEmpty) throw new Exception("Put on non-empty SyncVar")

    else {

      value = Some(x)

      isEmpty = false

    }

  }

}


查看完整回答
反對 回復(fù) 2021-07-29
  • 1 回答
  • 0 關(guān)注
  • 158 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號