Skip to content

add answers to exercise 1-7 chapter 3 #16

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Sep 14, 2015

Conversation

ryblovAV
Copy link
Contributor

Hi @axel22, pls check this answers


var r = new AtomicReference[List[T]](List.empty[T])

def push(x: T): Unit = {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can require that this method is tail recursive, to avoid the danger of a stack overflow.

@axel22
Copy link
Member

axel22 commented Aug 31, 2015

Thanks for the PR!
I've left mostly minor comments, except the concurrent sorted list exercise -- there the goal is to implement an actual mutable linked list, in which tail references are AtomicReference objects.

@ryblovAV
Copy link
Contributor Author

ryblovAV commented Sep 1, 2015

Hi @axel22,
Thanks for replying
pls see my changes

P.S.
I also have added the answers to the exercise 1-8 for chapter 4,
pls see

class ConcurrentSortedList[T](implicit val ord: Ordering[T]) {

private val h: AtomicReference[Option[T]] = new AtomicReference[Option[T]](None)
private val t: AtomicReference[Option[ConcurrentSortedList[T]]] = new AtomicReference[Option[ConcurrentSortedList[T]]](None)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would model the list with the following inner class:

class Node(val head: T) {
  val tail = new AtomicReference[Option[Node]](None)
}

The ConcurrentSortedList would then contain a single reference:

val root = AtomicReference[Option[Node]](None)

This would make the implementation of the add method simpler, and easier to reason about. There would be no need to call set in addToTail, since you set it only once anyway.

@ryblovAV
Copy link
Contributor Author

ryblovAV commented Sep 8, 2015

Thanks for replying,
I'll try to fix code according to your recommendations

@ryblovAV
Copy link
Contributor Author

ryblovAV commented Sep 9, 2015

Hi @axel22 ,
pls see

@axel22
Copy link
Member

axel22 commented Sep 14, 2015

LGTM - thanks a lot for following up!

axel22 added a commit that referenced this pull request Sep 14, 2015
add answers to exercise 1-7 chapter 3
@axel22 axel22 merged commit 9aaa3fa into concurrent-programming-in-scala:master Sep 14, 2015
@ryblovAV
Copy link
Contributor Author

thank you for your time

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants