package main
import (
"fmt"
"sync/atomic"
"time"
)
func Chann(ch chan int,addr *int32,exitCh chan bool) {
for {
ret := atomic.LoadInt32(addr)
if ret == 1 {
break
}
ch <- 100
time.Sleep(time.Second)
}
close(ch)
println("线程安全退出")
exitCh <- true
}
func timeOut(stopCh chan bool){
time.Sleep(time.Second * 3)
stopCh <- true
}
func test(count int) {
println("第",count,"次测试")
ch := make(chan int)
var n int32 = 0
stopCh := make(chan bool)
exitCh := make(chan bool)
go Chann(ch,&n,exitCh)
go timeOut(stopCh)
for {
select {
case c,err := <-ch:
if !err {
fmt.Println("读管道出错 1",err)
}else {
fmt.Println("Recvice 1", c)
}
case s,err := <-ch:
if !err {
fmt.Println("读管道出错 2",err)
}else {
fmt.Println("Recvice 2", s)
}
case _ = <-stopCh:
fmt.Println("超时")
atomic.AddInt32(&n,1)
case _ = <-exitCh:
fmt.Println("安全退出")
goto end
}
}
end:
}
type Phone struct {
number string
}
func (phone *Phone) setNumber(num string) {
phone.number = num
}
//func (phone Phone) setNumber(num string) {
// phone.number = num
//}
func (phone *Phone) call() {
fmt.Println("输出",phone.number)
}
type DeviceInfo struct {
DeviceType string
SystemNum string
}
type HuaweiPhone struct {
Phone
deviceinfo DeviceInfo
}
func NewHuaweiPhone() *HuaweiPhone {
return &HuaweiPhone{deviceinfo: DeviceInfo{DeviceType: "1",SystemNum: "10.1"}}
}
type TextFun interface {
SendText ()
}
func (phone HuaweiPhone) SendText() {
fmt.Println("华为手机发送短信",phone.number)
}
func main() {
var phone1 = new(Phone)
var phone2 = new(Phone)
phone1.setNumber("17712420000")
phone2.setNumber("18812420000")
phone1.call()
phone2.call()
huawei := NewHuaweiPhone()
huawei2 := NewHuaweiPhone()
huawei.deviceinfo.SystemNum = "10.01"
huawei.number = "1234"
huawei2.number = "456"
huawei.call()
huawei.SendText()
huawei2.SendText()
//for i := 1; i <= 10; i++ {
// test(i)
//}
}