zoukankan      html  css  js  c++  java
  • java network programming

    第五章
     5。2的几个例子展示了如何从线程返回结果

    特别是后来几种。。

    Notice the addition of the calculateDigest( ) method to start the thread. You might logically think that this belongs in a constructor.However, starting threads in a constructor is dangerous, especially threads that will call back to the originating object. There's a race condition here that may allow the new thread to call back before the constructor is finished and the object is fully initialized. It's unlikely in this case, because starting the new thread is the last thing this constructor does. Nonetheless, it's at least theoretically possible. Therefore, it's good form to avoid launching threads from constructors.这句话很重要,对5-8的解释


    5.3
     Synchronization is only a partial lock on an object. Other methods can use the synchronized object if they do so blindly, without attempting to synchronize on the object.

    几种不需要同步的primitive,String,一般来说构造函数也是不需要的(除了两种情况(The most likely issue is if a constructor depends on another object in another thread that may change while the constructor runs, but that's uncommon. There's also a potential problem if a constructor somehow passes a reference to the object it's creating into a different thread, but this is also uncommon.))。

    5.5
    对blocking有些注意

    Neither blocking on I/O nor blocking on a lock will release any locks the thread already possesses. For I/O blocks, this is not such a big deal, since eventually the I/O will either unblock and the thread will continue or an IOException will be thrown and the thread will then exit the synchronized block or method and release its locks. However, a thread blocking on a lock that it doesn't possess will never give up its own locks. If one thread is waiting for a lock that a second thread owns and the second thread is waiting for a lock that the first thread owns, deadlock results
    也就是说要注意同步时锁定的对象。线程是不会自己放弃锁的

    对yield方法只有一种用法

    Making a thread yield is quite simple in practice. If the thread's run( ) method simply consists of an infinite loop, just put a call to Thread.yield( ) at the end of the loop.

  • 相关阅读:
    OnFileOpen与OnOpenDocument(转)
    Wpf应用程序进入全屏和退出全屏
    在WPF中使用Emgu加载Image<,>图像的两种方法
    C#中ListBox控件重绘Item项
    sdut2404 Super Prime ACM算法设计
    真彩色制式下IplImage转成CBitmap格式
    【转】中缀表达式转换为后缀表达式
    MFC中CImage的简单复制方法 (Copy CImage)
    齐鲁软件大赛尖峰时刻团队
    Priest John's Busiest Day HDU2491 ACM算法设计
  • 原文地址:https://www.cnblogs.com/goodloop/p/84983.html
Copyright © 2011-2022 走看看