• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Threads,serialisation, static variables - java basic doubts

 
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[color=blue]
I have following doubts

1.what are the disadvantages in usage of Runnable interfaces over extending a Thread class to create a thread?

2. Can static variables in a class be searialised?

3.If a class A has two variables AB,AC

A subclass of A ie B has two varaibles BC,BD and also it implements serializable interface

a method is called on a object of type B to searilize it ? will it work correctly or cause erorrs as its parent A doesn't implement serializable interface
 
Sheriff
Posts: 22821
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Abhi Venu wrote:1.what are the disadvantages in usage of Runnable interfaces over extending a Thread class to create a thread?


Are there any? Perhaps the usage of an extra object is the only one - one Runnable plus one Thread instead of only one Thread. For the rest there are only advantages. See also Extending Thread Vs Implementing Runnable.

2. Can static variables in a class be searialised?


If they are serializable, but only if you serialize them yourself. They aren't serialized when you serialize an instance (after all, it's not an instance variable), but nothing prevents you from using writeObject with a static field.

3.If a class A has two variables AB,AC

A subclass of A ie B has two varaibles BC,BD and also it implements serializable interface

a method is called on a object of type B to searilize it ? will it work correctly or cause erorrs as i[/color]ts parent c


If class A is not serializable then AB and AC will not be serialized, and not be serialized. In this case the constructor without parameters will be called for class A. If there is none then an exception will be thrown.
Try it:
Try this, then remove the first constructor from class A and try again.
 
Abhi Venu
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks alot for the prompt and beautiful descriptive reply
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have extended Thread when I wanted to use ThreadGroup to keep track of the state of a bunch of worker Threads.

Bill
 
Rancher
Posts: 618
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

1.what are the disadvantages in usage of Runnable interfaces over extending a Thread class to create a thread?


You only get to extend one class. If you extend Thread then you cannot extend something else that may help you.
 
Rob Spoor
Sheriff
Posts: 22821
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So that's an advantage of using Runnable, not a disadvantage.
 
LOOK! OVER THERE! (yoink) your tiny ad is now my tiny ad.
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic