java java中的序列化和同步有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2599090/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 21:58:38  来源:igfitidea点击:

What is the difference between serialization and synchronization in java?

javamultithreading

提问by Praveen

What is the difference between serialization and synchronization in java? I need an explanation or a tutorial.

java中的序列化和同步有什么区别?我需要解释或教程。

回答by Kris

Synchronization refers to multi-threading. A synchronized block of code can only be executed by one thread at a time.

同步是指多线程。同步代码块一次只能由一个线程执行。

Serialization refers to converting objects to bitstreams either for storage or transmission. The act of serialization encodes the data according to specific rules. This bitstream can then be deserialized later or on a remote system that receives it. For serialization to work the class definition must match (i.e. you need to be using the same version of the class or one that is guaranteed to be compatible) and the class must implement the Serializableinterface.

序列化是指将对象转换为比特流以进行存储或传输。序列化行为根据特定规则对数据进行编码。然后可以稍后或在接收它的远程系统上反序列化该比特流。要使序列化工作,类定义必须匹配(即您需要使用相同版本的类或保证兼容的版本)并且类必须实现Serializable接口。

More on serialization.

更多关于序列化

More on synchronization

更多关于同步

回答by polygenelubricants

Synchronization is a concurrency issue, e.g. how do you coordinate access to an object from multiple threads.

同步是一个并发问题,例如,您如何协调多个线程对一个对象的访问。

Here an arrow represents access.

这里的箭头代表访问。

                            s
 [thread1] ---------------> y
                            n [shared object]
 [thread2] ---------------> c
                            h

Serialization is the conversion of data structures and objects into a sequence of bits that you can store/transmit and then convert back to data structures and objects.

序列化是将数据结构和对象转换为可以存储/传输的位序列,然后再转换回数据结构和对象。

Here an arrow represents conversion.

这里的箭头代表转换。

            deserialization
           <---------------
  [object]                  [binary]
           --------------->
            serialization

This is most useful when the deserialization happens at another place and/or time.

当反序列化发生在另一个地方和/或时间时,这是最有用的。

回答by Ramya P

Synchronization makes sure that only one thread will be executing at a time and hence no chance of getting deadlock situation.

同步确保一次只执行一个线程,因此不会出现死锁情况。

Serialization refers to storing the state of an object.For example,we can take a video game.If we pause the game and continue it later it will resume the game.That means the state and level have got stored here.

序列化是指存储一个对象的状态。例如,我们可以拿一个视频游戏。如果我们暂停游戏并稍后继续游戏,它将恢复游戏。这意味着状态和级别已经存储在这里。

回答by Nir Levy

serialization is taking an object and dumping it to something that is outside the program's scope (e.g. a string or an xml file)

序列化正在获取一个对象并将其转储到程序范围之外的内容(例如字符串或 xml 文件)

synchronization is a concept of having different running threads in sync with each other so that they do not, for example, use a shared resource at the same time.

同步是使不同的运行线程彼此同步的概念,例如,它们不会同时使用共享资源。

As far as I know these terms have very little in common (unless you have a question about how to synchronize two threads that need on serialize the same object)

据我所知,这些术语几乎没有共同之处(除非您对如何同步需要序列化同一个对象的两个线程有​​疑问)