티스토리 뷰
MainActivity.kt
package com.jwsoft.kotlinproject
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
class MainActivity : AppCompatActivity() {
var name: String? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
Thread {
for (i in 1 until 10) {
whoAmI("혀가 길지 않은 개발자")
}
}.start()
Thread {
for (i in 1 until 10) {
whoAmI("혀가 긴 개발자")
}
}.start()
}
fun whoAmI(name: String) {
this.name = name
Thread.sleep(100)
if (!this.name.equals(name)) {
Log.e("", this.name + " != " + name)
}
}
}
MainActivity.kt
package com.jwsoft.kotlinproject
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
class MainActivity : AppCompatActivity() {
var name: String? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
Thread {
for (i in 1 until 10) {
whoAmI("혀가 길지 않은 개발자")
}
}.start()
Thread {
for (i in 1 until 10) {
whoAmI("혀가 긴 개발자")
}
}.start()
}
private fun whoAmI(name: String) {
synchronized(this) {
this.name = name
Thread.sleep(100)
if (!this.name.equals(name)) {
Log.e("", this.name + " != " + name)
}
}
}
}
MainActivity.kt
package com.jwsoft.kotlinproject
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
class MainActivity : AppCompatActivity() {
var food: String? = null
private lateinit var person: MainActivity
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
person = MainActivity()
Thread {
for (i in 1 until 10) {
person.eatPizza()
}
}.start()
Thread {
for (i in 1 until 10) {
person.eatHamburger()
}
}.start()
}
private fun eatPizza() {
this.food = "Pizza"
var waiting: Long = (Math.random() * 100).toLong()
Thread.sleep(waiting)
if (this.food != "Pizza") {
Log.e("", this.food + " != " + "Pizza")
}
}
private fun eatHamburger() {
this.food = "Hamburger"
var waiting: Long = (Math.random() * 100).toLong()
Thread.sleep(waiting)
if (this.food != "Hamburger") {
Log.e("", this.food + " != " + "Hamburger")
}
}
}
MainActivity.kt
package com.jwsoft.kotlinproject
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
class MainActivity : AppCompatActivity() {
var food: String? = null
private lateinit var person: MainActivity
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
person = MainActivity()
Thread {
for (i in 1 until 10) {
person.eatPizza()
}
}.start()
Thread {
for (i in 1 until 10) {
person.eatHamburger()
}
}.start()
}
private fun eatPizza() {
synchronized(this) {
this.food = "Pizza"
var waiting: Long = (Math.random() * 100).toLong()
Thread.sleep(waiting)
if (this.food != "Pizza") {
Log.e("", this.food + " != " + "Pizza")
}
}
}
private fun eatHamburger() {
synchronized(this) {
this.food = "Hamburger"
var waiting: Long = (Math.random() * 100).toLong()
Thread.sleep(waiting)
if (this.food != "Hamburger") {
Log.e("", this.food + " != " + "Hamburger")
}
}
}
}
synchronized 추가
MainActivity.kt
package com.jwsoft.kotlinproject
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
class MainActivity : AppCompatActivity() {
var food: String? = null
private lateinit var person1: MainActivity
private lateinit var person2: MainActivity
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
person1 = MainActivity()
person2 = MainActivity()
Thread {
for (i in 1 until 10) {
person1.eatPizza()
}
}.start()
Thread {
for (i in 1 until 10) {
person2.eatHamburger()
}
}.start()
}
private fun eatPizza() {
synchronized(this) {
this.food = "Pizza"
var waiting: Long = (Math.random() * 100).toLong()
Thread.sleep(waiting)
if (this.food != "Pizza") {
Log.e("", this.food + " != " + "Pizza")
}
}
}
private fun eatHamburger() {
synchronized(this) {
this.food = "Hamburger"
var waiting: Long = (Math.random() * 100).toLong()
Thread.sleep(waiting)
if (this.food != "Hamburger") {
Log.e("", this.food + " != " + "Hamburger")
}
}
}
}
객체 2개 생성
private lateinit var person1: MainActivity
private lateinit var person2: MainActivity
MainActivity.kt
package com.jwsoft.kotlinproject
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
class MainActivity : AppCompatActivity() {
var list: ArrayList<Int> = ArrayList()
var tryCount: Int = 1
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
Log.e("", "Start!")
var thread1 = Thread {
for (i in 1..10000) {
pushNumber(i)
}
}
thread1.start()
var thread2 = Thread {
for (i in 1..10000) {
pushNumber(i)
}
}
thread2.start()
thread1.join()
thread2.join()
Log.e("", "End! " + list.size)
}
private fun pushNumber(number: Int) {
Log.e("", (tryCount++).toString())
if (!list.contains(number)) {
list.add(number)
}
}
}
카운트 총합이 20000미만인 이유는 첫 번째 스레드가 49에서 50으로 증가하는 타이밍에 두 번째 스레드에서 48에서 49로 증가하게 되면 카운트 총합은 감소하게 된다.
MainActivity.kt
package com.jwsoft.kotlinproject
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
class MainActivity : AppCompatActivity() {
var list: ArrayList<Int> = ArrayList()
var tryCount: Int = 1
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
Log.e("", "Start!")
var thread1 = Thread {
for (i in 1..10000) {
pushNumber(i)
}
}
thread1.start()
var thread2 = Thread {
for (i in 1..10000) {
pushNumber(i)
}
}
thread2.start()
thread1.join()
thread2.join()
Log.e("", "End! " + list.size)
}
private fun pushNumber(number: Int) {
synchronized(this) {
Log.e("", (tryCount++).toString())
if (!list.contains(number)) {
list.add(number)
}
}
}
}
MainActivity.kt
package com.jwsoft.kotlinproject
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
class MainActivity : AppCompatActivity() {
var count1 = 1
var count2 = 1
lateinit var stopWatch: MainActivity
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
stopWatch = MainActivity()
var startTime: Long = System.currentTimeMillis()
var thread1 = Thread {
for (i in 1..10000) {
stopWatch.addCount1()
}
}
var thread2 = Thread {
for (i in 1..10000) {
stopWatch.addCount2()
}
}
thread1.start()
thread2.start()
thread1.join()
thread2.join()
var endTime: Long = System.currentTimeMillis()
var elapsedTime = endTime - startTime
Log.e("소요시간 : ", elapsedTime.toString())
}
private fun addCount1() {
synchronized(this) {
Log.e("", (this.count1++).toString())
}
}
private fun addCount2() {
synchronized(this) {
Log.e("", (this.count2++).toString())
}
}
}
MainActivity.kt
package com.jwsoft.kotlinproject
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
class MainActivity : AppCompatActivity() {
var count1 = 1
var count2 = 1
lateinit var stopWatch: MainActivity
var object1: Any = Object()
var object2: Any = Object()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
stopWatch = MainActivity()
var startTime: Long = System.currentTimeMillis()
var thread1 = Thread {
for (i in 1..10000) {
stopWatch.addCount1()
}
}
var thread2 = Thread {
for (i in 1..10000) {
stopWatch.addCount2()
}
}
thread1.start()
thread2.start()
thread1.join()
thread2.join()
var endTime: Long = System.currentTimeMillis()
var elapsedTime = endTime - startTime
Log.e("소요시간 : ", elapsedTime.toString())
}
private fun addCount1() {
synchronized(object1) {
Log.e("", (this.count1++).toString())
}
}
private fun addCount2() {
synchronized(object2) {
Log.e("", (this.count2++).toString())
}
}
}
'Android > Kotlin' 카테고리의 다른 글
[Kotlin] Coroutine (2) (0) | 2020.07.29 |
---|---|
[Kotlin] Thread.join() (0) | 2020.07.27 |
[Kotlin] Generics (0) | 2020.07.27 |
[Kotlin] Coroutine (1) (0) | 2020.07.23 |
[Kotlin] MVVM (0) | 2020.07.21 |
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- XML
- Intent
- ViewModel
- 혀가 길지 않은 개발자
- handler
- View
- ViewPager2
- JSONObject
- Design Pattern
- java
- Livedata
- JSONArray
- activity
- DataBinding
- 코틀린
- ArrayList
- 자바
- coroutine
- recyclerview
- CoordinatorLayout
- Android
- fragment
- MVVM
- 안드로이드 #코틀린 #Android #Kotlin
- 안드로이드
- James Kim
- TabLayout
- Architecture Pattern
- Kotlin
- Vue.js #Vue.js + javascript
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | |||
5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 | 25 |
26 | 27 | 28 | 29 | 30 | 31 |
글 보관함