• 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.

  • 相关阅读:
    VS2013无法启动 IIS Express Web解决办法
    浅谈Java中switch分支语句
    instanceof运算符与引用变量的强制类型转换
    Java语言中null与" "的区别
    Java字符串无意识的递归
    不同编程语言实现输出“HelloWorld!”
    用 C# 来守护 Python 进程
    Python核心技术与实战 笔记
    Python3 系列之 并行编程
    Django + Gunicorn + Nginx 部署之路
  • 原文地址:https://www.cnblogs.com/goodloop/p/84983.html
Copyright © 2020-2023  润新知